VBA Red Close Event did not close workbook properly - excel

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

Related

Close form withot selectionchange event

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

Excel Userform Run-Time Error when trying to launch a 2nd time after initial 'Cancel' or close on red 'X'

Problem: I am building a Userform that has a 'Submit' and 'Cancel' button. I want the entire form to clear any entered data and close the form if the user hits the 'Cancel' button but I also am trying to build in the same functionality if the user hits the red 'X' in the top right corner. I'm unclear where I need to unload the form. I currently have it placed within the btnCancel_Click() method and I'm able to launch the form, enter some data and hit Cancel and it will close the form down.
But when I try to re-launch the form a 2nd time I get an error (I attached a picture of that message) that says
"Run-Time error '-2177418105 (80010007): Automation Error - The Callee (server [not server application]) is not available and disappeared; all connections are invalid. The call may have executed.
If I remove Unload Me from btnCancel_Click() then the form can close and re-open just fine, but any data I entered the first time will still be on the form and isn't cleared properly. I'm wondering if this is an Unload Me error or do I need to reset all form controls when I initialize the form?
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' how was the form closed?
' vbFormControlMenu = X in corner of title bar
If CloseMode = vbFormControlMenu Then
' cancel normal X button behavior
Cancel = True
' run code for click of Cancel button
btnCancel_Click
End If
End Sub
'******************************************************************
Private Sub btnCancel_Click()
mbCancel = True
Me.Hide
Unload Me
End Sub
'*********************************************************************
Private Sub UserForm_Initialize()
'Populate values for 2 combo boxes
lastEmp = Sheets("Form_Ref").Cells(Rows.Count, 1).End(xlUp).Row
Me.cmbBoxEmpName.List = Sheets("Form_Ref").Range("A2:A" & lastEmp).Value
lastBld = Sheets("Form_Ref").Cells(Rows.Count, 2).End(xlUp).Row
Me.cmbBoxBuildingName.List = Sheets("Form_Ref").Range("B2:B" & lastBld).Value
End Sub
'******************************************************************
Public form As New CheckOutForm
Sub testFormOptions()
'Button pressed within Excel will start program and show the userform
form.Show
End Sub
This is the easiest quick and dirty solution:
Delete Public form As New CheckOutForm from the code. Then add it in the testFormOptions():
Sub testFormOptions()
Dim form As New CheckOutForm
form.Show
End Sub
Some not-that-good VBA books/tutorials would even go a bit like this, but this is brutal:
Sub testFormOptions()
CheckOutForm.Show
End Sub
Anyway, now the problem with the predefined values in the form is solved.
For the clean and not-so-easy solution, consider writing a MVC framework around the form:
https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form
this blogpost (disclaimer - mine!), which pretty much says what the above link proposes, but it does not have the errors from the question.
the old StackOverflow tutorial for UserForms
If you execute Unload, you destroy the form object. With other words, your (global) variable form gets invalid and if you issue a new form.show, you get the runtime error.
When, on the other hand, you just unhide the form, the form-object stays valid (it's just currently not visible) and all controls keep their value.
Either you do some housekeeping by resetting all controls when a form is displayed (use the UserForm_Activate-event), or you have to create a new form-object every time you want to display it (I would strongly advice not to use the name form as variable name to avoid confusion).
Sub testFormOptions()
dim myForm as CheckOutForm
myForm.Show
End Sub

Uncheck a checkbox if the (X)-Close Button of the Userform is pressed

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

how to make exe for vba-excel form

is there any way to create exe or something like that for my userform made in excel vba.
i tried to open it everytime the workbook gets open but once i exit the form,i get back to the sheet1(where the data entered by user is getting dumped) and then the data entry person gets confused on how to get the form back.instead of it i want to have some way to hide the sheet from the user or simply want to give them the userform to enter the data.
any mechanism to open the form with any shortcuts?
Yup!
Use Application.visible = False and then reset it. Put this in your userform
Private Sub UserForm_Initialize()
Application.Visible = False
End Sub
'
'~~> Rest of the code
'
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ThisWorkbook.Close SaveChanges:=True '<~~ Save workbook before closing
DoEvents
Application.Visible = True
End Sub

Why does the VBA Main form close by Sub Form's 'x' close button event?

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.

Resources