I trying to use this add in https://www.excelcampus.com/wp-content/uploads/2022/02/The_List_Search_Add-in_for_Excel.zip
but problem if i select down
and post some fields and press close button, form appears multiple times equals to posted previous fields.
So how to prevent toggle form show and just close form on closee button instead of multiple time fire onchange event?
This code also just ignoring by vba
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
'Cancel = True
Unload Me
Exit Sub
End If
End Sub
Unload Me do nothing, and form not closing
Related
I have written a vba in which when a user opens the xlsm file, vba will set Application.Visible = False and shows user a userform to enter their credentials before setting Application.Visible = True become for user to use the excel.
While testing various scenarios, there is one that seems odd.
If user closed the Userform using the red cross at the top right corner before they successfully entered their credentials, and when I double click on the same xlsm file again, the Application becomes visible without the Userform.
I suspect when user closed with the red cross, only the UserForm is closed and the application was not. How can I make the red cross close the application so that when I double click on the xlsm again, it will run the marco for Userform to pop out?
Update:
The following did close the workbook with the red cross but I noticed even if user close the UserForm with CommandButton, it will still close the workbook. Is there a sub that only detects red cross?
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Detecting user closing with top cross
Sheet4.Range("B4") = CloseMode
If CloseMode = 1 Then Cancel = 0
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
So the doc for the _QueryClose event is here: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/queryclose-event
I am not familiar with your code, but you probably want something like this:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'Detecting user closing with top cross
If CloseMode = 0 Then
' 0 = vbFormControlMenu (the [X] was clicked)
Cancel = 1 ' this keeps the form from closing
'(or whatever you want to do here)
End if
End Sub
1st, what i want to do:
I have a main UserForm, on that form i have a button to show secondary UserForm. When i click on that button, i want main form to be hidden. When i'm done with with work on the secondary form, i want to close it and show the main form again.
2nd, what i have so far (relevant code):
Main form code:
Private Sub createFastButton_Click()
Me.Hide
formSec.Show
End Sub
Secondary form code:
Private Sub cancelButton_Click()
Me.Hide
formMain.Show
End Sub
Private Sub UserForm_Terminate()
formMain.Show
End Sub
3rd, the problem:
If i use ControlButtons to navigate between forms, all works as intended. But if i use the "X" close window button on the secondary form and try to open it again from main form, it loses all functionality (main form works fine). It just shows the secondary form as i see it, when i use "View Object" in VBA editor. None of the buttons work and none of the intended boxes and labels are filled. Even the "X" button doesn't work. For me it seems obvious, that the problem is with unloading of secondary form. I tried to replace Me.Hide in secondary form with Unload Me and exactly same thing happens, as if i press the "X" button. So i need to do something with Sub UserForm_Terminate(), i tried to add Me.Hide there and as expected, it did nothing.
I there a solution to my problem?
Thx in advance.
main routine, calling Main Form:
With New formMain
.Show
End With
main form, calling secondary form
Private Sub createFastButton_Click()
Me.Hide
With New formSec
.Show
End With
Me.Show
End Sub
secondary form
Private Sub cancelButton_Click()
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then Cancel = True
End Sub
no need for UserForm_Terminate() in secondary form, unless for unsaid needs
Clicking the "X" raises a QueryClose event that you need to handle, e.g.:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
Me.Hide
' Add more code here to respond to form close event
End If
End Sub
See here for a very helpful explanation.
I have a userform that is activated whenever I check a checkbox that is on the sheet, and if I uncheck it, it is hidden from the screen. The problem is that when I press the red close (X) button from the userform, the checkbox doesn't uncheck, but it should, since the userform is no longer on the screen. I don't know how to fix that.
Though it might not be best practice you could use the following code in the QueryClose event
Private Sub UserForm_QueryClose(Cancel As Integer _
, CloseMode As Integer)
' Prevent the form being unloaded
If CloseMode = vbFormControlMenu Then Cancel = True
' Hide the Userform
Hide
ThisWorkbook.Worksheets(1).Shapes("Checkbox1").OLEFormat.Object.Value = 0
End Sub
I assume you do not use an ActiceX-Control and the name of your checkbox is CheckBox1
A slightly better way might be to use the following code in a normal module for the checkbox instead of the above code in the userform module. In this way the form does not need to know about a checkbox which called it.
Sub Checkbox_code()
Dim f As UserForm1
Dim b As CheckBox
Set f = New UserForm1
Set b = ThisWorkbook.Worksheets(1).Shapes("Checkbox1").OLEFormat.Object
If b.Value = xlOn Then
f.Show
b.Value = xlOff
End If
End Sub
i've solved it:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
ThisWorkbook.Worksheets("Sheet1").CheckBox1.Value = False
End If
End Sub
I have two forms, a login form to authenticate users to enter the system and a main form to work with if the authentication is successful. I work with an access database to search for the valid users and also to populate lists in the main form.
And here is the code for that:
Private Sub CancelCommandButton_Click()
CloseDatabase
Unload Me
End Sub
Private Sub ConfirmCommandButton_Click()
...
End Sub
Private Sub UserForm_Initialize()
ConnectDatabase
End Sub
Private Sub OpenMainForm()
Unload Me
MainForm.Show
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
End If
End Sub
And here is the code for managing main form load-up and close:
Public Sub UpdateControls()
PopulateUserList
End Sub
Private Sub UserForm_Activate()
sTag = Split(Me.Tag, "|")
If sTag(0) <> "1" Then
Me.MainFormMultiPage.Pages(0).Enabled = False
Me.MainFormMultiPage.Value = 1
Else
Me.MainFormMultiPage.Pages(0).Enabled = True
Me.MainFormMultiPage.Value = 0
UpdateControls
End If
UserLabel1.Caption = sTag(1)
UserLabel2.Caption = sTag(1)
UserLabel3.Caption = sTag(1)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
' Tip: If you want to prevent closing UserForm by Close (×) button in the right-top corner of the UserForm, just uncomment the following line:
' Cancel = True
Dim answer As Integer
answer = MsgBox("ÂíÇ ãØãÆäíÏ ˜å ãí ÎæÇåíÏ ÇÒ ÓÇãÇäå ÎÇÑÌ ÔæíÏ¿", vbYesNo + vbQuestion, "ÎÑæÌ ÇÒ ÓÇãÇäå")
If answer = vbYes Then
CloseDatabase
Else
Cancel = True
End If
End If
End Sub
Private Sub UserForm_Terminate()
loginForm.Show
End Sub
When I close the main form by clicking 'X' button, the login form reappears and the main form is closed; But when I login again (preferably using the same credentials) the main form is displayed but totally unresponsive (No list population, 'X' button doesn't work or any other controls in the form).
What should I do? Does the code in the UserForm_CloseQuery() unload the main form and all of its macros and I can't get the required events back to function or am I missing something?
It's not a time I started coding VBA and I can't make the head or tail of it easily when new problems arise. Any help would be appreciated. Thanks
Assuming your forms are modal, change the login form code to:
Private Sub OpenMainForm()
Me.Hide
MainForm.Show
Me.Show
End Sub
and remove the Userform_Terminate code from the main form. That will mean that the login form automatically shows when the main form is closed.
Hey so I am working on some forms in VBA and am having some issues. I open other forms from my main form, but when I exit one of the other forms using the red "x" button, it closes my main form as well, not just the other form.
How can I prevent this?
You should use a code that only closes the sub form.
e.g. your sub form name is SubForm1
DoCmd.Close acForm, "SubForm1"
But since you are using x-button to close the sub form, you can use QueryClose Event.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = True
End if
End Sub
PS: helpful if you could post your code by editing your question.
I know this is old but check the setting of the forms "Showmodal" property fixed it for me.