If.. Else help

  • Thread starter Thread starter Bill Davis
  • Start date Start date
B

Bill Davis

I am trying to use this code in the On Close Event of my main form, but
I get can "variable not defined" Compile error. I am new to and VB so
any help would be appreciated.
Thank in advance


If MsgBox("Are you sure you want to exit?", 65, " Database") = vbOK Then
DoCmd.RunCommand acCmdExit
ElseIf vbCancel Then
Cancel = True
End If
 
I am trying to use this code in the On Close Event of my main form, but
I get can "variable not defined" Compile error. I am new to and VB so
any help would be appreciated.
Thank in advance


If MsgBox("Are you sure you want to exit?", 65, " Database") = vbOK Then
DoCmd.RunCommand acCmdExit
ElseIf vbCancel Then
Cancel = True
End If

Since 65 = vbOkCancel, OK and Cancel are the only possible results.
Just change "ElseIf vbCancel Then" to "Else".

Or, use the Select Case construct (especially if you'll ever have
three branches); e.g.

Dim iAns As Integer
iAns = MsgBox("Are you sure you want to exit?", vbYesNoCancel)
Select Case iAns
Case vbYes
<do something for clicking Yes>
Case vbNo
<do something else for clicking No>
Case vbCancel
<do something else again>
Case Else
<error message, shouldn't happen>
End Select


John W. Vinson[MVP]
 
I'm not sure what buttons you want on your MessageBox but try this code:

If MsgBox("Are you sure you want to exit?", vbYesNo, " Database") = vbYes
Then
DoCmd.RunCommand acCmdExit
Else
Cancel = True
End If
 
I am trying to use this code in the On Close Event of my main form, but
I get can "variable not defined" Compile error. I am new to and VB so
any help would be appreciated.
Thank in advance

If MsgBox("Are you sure you want to exit?", 65, " Database") = vbOK Then
DoCmd.RunCommand acCmdExit
ElseIf vbCancel Then
Cancel = True
End If

The form's Close event does not have a Cancel argument.
If you are trying to confirm the user closing the form (and not
closing it if No is clicked), use the Form Unload event, not the Close
event.

Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Are you sure you want to exit?", vbYesNo, " Database") =
vbNo Then
Cancel = True
End If
End Sub

The form will close only if Yes is selected.
 
Bill said:
I am trying to use this code in the On Close Event of my main form, but
If MsgBox("Are you sure you want to exit?", 65, " Database") = vbOK Then
DoCmd.RunCommand acCmdExit
ElseIf vbCancel Then
Cancel = True
End If

Yes,as ruralguy have said.

You don't need the "Cancel = True", do something like in the On Close Event.

If MsgBox("Are you sure you want to exit?", 65, " Database") = vbOK Then
DoCmd.RunCommand acCmdExit
Else
Exit sub 'use the exit sub to cancel the event
End If
 
Oops,

Was fredg, not ruralguy.
Yes,as ruralguy have said.

You don't need the "Cancel = True", do something like in the On Close Event.

If MsgBox("Are you sure you want to exit?", 65, " Database") = vbOK Then
DoCmd.RunCommand acCmdExit
Else
Exit sub 'use the exit sub to cancel the event
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top