Userform - Does Not Completely Close

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
105
Office Version
  1. 365
Platform
  1. Windows
I close a userform when the user clicks the x:

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Unload userformNav
    'Unload me ' also tried this 
End Sub
The problem is that the form still seems to be active somewhere. When the user clicks a button it first checks to see if the userform is active before loading. The form is not equal to nothing, as it is prior to initially loading.

Code:
If (userformNav Is Nothing = False) Then
    MsgBox "program already running"
    Exit Sub
End If
Why, after closing the form with the first block of code, would the if test above return true?

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
when checking for Is Nothing to be False, you need to use a different syntax, like this:
Code:
If Not userformNav Is Nothing Then
    MsgBox "program already running"
    Exit Sub
End If
Not sure if that caused the issue, but I would bet some money on it :)
 
Upvote 0
Thanks for the reply, but that is not the issue.

I should have mentioned that the userform is set to nothing only after the reset button is pressed from the vba menu.
Is there a way for vba to call the reset functionality?
I have tried a few things such as this with no luck:

Code:
DoCmd.RunCommand acCmdReset
Thanks
 
Upvote 0
How and where do you Load or Show the userform at first?

Unless you want special exitbehaviour, you shouldn't need to put Unload Me code in the QueryClose eventhandler. Have you tried without it? Is your form modal or modeless?
 
Upvote 0
If you run this bit of code at any time, it will return False.
Code:
Dim aForm As UserForm1

Set aForm = New UserForm1
MsgBox aForm Is Nothing
Mearly by refering to the userform, VBA creates an instance of the form.
You could test
Code:
If UserForms.Count = 0 Then MsgBox "No loaded Userforms"
or to test for a particular uf.
Code:
Dim someForm As Object
Dim ufFlag As Boolean

For Each someForm In UserForms
    If someForm.Name = "UserForm1" Then ufFlag = True
Next someForm

If ufFlag Then
    MsgBox "UserForm1 is loaded"
Else
    MsgBox "UserForm1 is not loaded"
End If
Excel has a few cases where a not nothing argument is around

Code:
Dim aForm As UserForm1

Set aForm = New UserForm1
aForm.Show

MsgBox (aForm Is Nothing) & vbCr & UserForms.Count: rem returns False,0
    
' aForm.Show: Rem trying to run this line will crash

similar to
Code:
Dim myRange As Range
Set myRange = Range("A1")

myRange.Delete

MsgBox myRange Is Nothing: Rem returns False
MsgBox myRange.Cells.Count: Rem code errors "Object variable not set"
 
Last edited:
Upvote 0
Thanks to both. I should be able to modify my code to iterate through the UserForms with the for each statement to have it work as expected.

I still do not understand the behavior though. As shown by the following sequence, the form is not nothing at #5, yet at #6 there is no userform in the group.

Message Box output at each point:
1) false
2) true
3) formNav
4) true
5) true
6) -does not access this msgbox-

Code:
Private Function DoesFormExist(theform As UserForm) As Boolean
    If theform Is Nothing Then
        DoesFormExist = False
    Else
        DoesFormExist = True
    End If
End Function


Dim aform As formNavigator
MsgBox "1. after variable definition: " & DoesFormExist(aform)

Set aform = New formNavigator
MsgBox "2. after set statement: " & DoesFormExist(aform)

For Each auserform In UserForms
MsgBox "3. after set - form present with name " & auserform.name
Next auserform

aform.Show vbModeless
MsgBox "4. after show: " & DoesFormExist(aform)

Unload aform
Dim exists As Boolean
If aform Is Nothing Then ' crashes if aform sent to function
exists = False
Else
exists = True
End If
MsgBox "5. after unload: " & exists

For Each auserform In UserForms
MsgBox "6. after unload - form present with name " & auserform.name
Next auserform
 
Upvote 0
Code:
Dim aForm as UserForm1

aForm = New UserForm1
aForm.Show
unLoad aForm

Rem Right Here
You could think of it that at RightHere, aForm is a variable that points to no object and neither does it point to the constant Nothing.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top