Reputation: 225
I made the code below from Excel 2016 VBA.
Private Sub useridno_AfterUpdate()
Dim strMsg As String
Dim ret_type As Integer
Dim strTitle As String
strTitle = "Wrong User ID Number!"
strMsg = " Would you like to try again?"
If Me.useridno.Value <> 1 Then
ret_type = MsgBox(strMsg, vbYesNo + vbCritical, strTitle)
Select Case ret_type
Case 6
Me.useridno.SetFocus
Me.useridno.Text = ""
Case 7
Unload Me
End Select
End If
End Sub
When I run it, it returns the following error:
This happens when I choose the "No" button in the message box. Why is this so?
Upvotes: 0
Views: 105
Reputation: 14373
Somewhere in your project you have a Show
command which displays the userform. From that moment forward the userform is in control. The code following the Show
command will resume when the userform is closed. The command to do so is Me.Hide
as @Storax has pointed out.
Your code has Unload Me
instead. The error occurs when the other procedure tries to refer to the userform after the Show
command. Your code might try to read some of the data from the form or there might be an innocuous Set UserForm = Nothing
. It is already unloaded and can't, therefore, be referred to anymore.
The correct way would be to unload the form in the calling procedure, following the Show
command and after you have retrieved all data from the form that you might want to use. Then, if you wish to explicitly release the form's object variable from memory you can do so.
Upvotes: 1