If Msg = <> vb no then.............NOT Working

J

JNariss

Hello,

I have a form which I included and option for users to "Exit Access". I
have put a code behind this to prompt the user that they actually want
to close Access but the code is not working properly. When I choose the
option "no" it closes Access. This is not what I want. I only want
Access to close if "Yes" is chosen.

My code is:

Private Sub cmdExitAccess_Click()
MsgBox "Are you sure you want to close this database?", vbYesNo,
"Confirm Action"
If Msg <> vbYes Then
DoCmd.Quit acQuitSaveAll
Elseif Msg <> vbNo Then
'Do Nothing
End If
End Sub

I have also tried it without including the code for vbNo:

Private Sub cmdExitAccess_Click()
MsgBox "Are you sure you want to close this database?", vbYesNo,
"Confirm Action"
If Msg <> vbYes Then
DoCmd.Quit acQuitSaveAll
End If
End Sub



And this did not work either. Anyone have any ideas on how to fix this
properly??

Thanks,
Justine
 
J

JNariss

I just wanted figured it out:


Private Sub cmdExit_Click()
MsgBox "Are you sure you want to close this database?", vbYesNo,
"Confirm Action"
If Msg <> vbNo Then
'Do Nothing
ElseIf Msg <> vbYes Then
DoCmd.Quit acQuitSaveAll
End If
End Sub

I changed the way the user would repsond by putting the code for "No"
before the code for a response of "yes" and it worked.
 
D

Dirk Goldgar

Hello,

I have a form which I included and option for users to "Exit Access".
I have put a code behind this to prompt the user that they actually
want to close Access but the code is not working properly. When I
choose the option "no" it closes Access. This is not what I want. I
only want Access to close if "Yes" is chosen.

My code is:

Private Sub cmdExitAccess_Click()
MsgBox "Are you sure you want to close this database?", vbYesNo,
"Confirm Action"
If Msg <> vbYes Then
DoCmd.Quit acQuitSaveAll
Elseif Msg <> vbNo Then
'Do Nothing
End If
End Sub

I have also tried it without including the code for vbNo:

Private Sub cmdExitAccess_Click()
MsgBox "Are you sure you want to close this database?", vbYesNo,
"Confirm Action"
If Msg <> vbYes Then
DoCmd.Quit acQuitSaveAll
End If
End Sub

And this did not work either. Anyone have any ideas on how to fix this
properly??

Thanks,
Justine

If that's your code, it's never going to work, because you aren't
assigning the return value of the MsgBox function to the variable "Msg".
You should write:

Msg = MsgBox( _
"Are you sure you want to close this database?", _
vbYesNo, _
"Confirm Action")

If Msg = vbYes Then
DoCmd.Quit acQuitSaveAll
End If

Are you sure you want to specify the acQuitSaveAll option? That means
that any design changes -- not data changes -- that the user has made to
open forms, datasheets, etc., will be saved without prompting. It has
no effect on data changes, which are saved automatically unless they
violate a data integrity constraint of some sort.
 
J

JNariss

I just figured it out!!!!!!!!!

With this code:

Private Sub cmdExit_Click()
Select Case MsgBox("Are you sure you want to close this database?",
vbYesNo, "Confirm Action")
Case vbNo
'Do Nothing. Default Behavior
Case vbYes
'Close and Quit Access
DoCmd.Quit acQuitPrompt
End Select
End Sub

But I will also take your code for future reference - I appreciate it.
I did take the advice on the "Quit" prompt - I did not know that it
referred to design changes.

Thanks,
Justine
 
D

Dirk Goldgar

I just figured it out!!!!!!!!!

With this code:

Private Sub cmdExit_Click()
Select Case MsgBox("Are you sure you want to close this database?",
vbYesNo, "Confirm Action")
Case vbNo
'Do Nothing. Default Behavior
Case vbYes
'Close and Quit Access
DoCmd.Quit acQuitPrompt
End Select
End Sub

Yes, I actually prefer that form, because you don't need to declare a
holding variable for the return value from MsgBox(). Well done.
 

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

Top