MsgBox with OK and Cancel buttons

G

Guest

How do I tell the OK and Cancel buttons to:
Allow the action if OK is clicked
Cancel the action if the Cancel button is clicked

In this case, the action is opening a form.

I know how to get both buttons on the Message box, I just can't figure out
how to get them to do something other than allowing the form to open

Thanks in advance to anyone who can help.
John
 
R

Rick Brandt

j0hnt52 said:
How do I tell the OK and Cancel buttons to:
Allow the action if OK is clicked
Cancel the action if the Cancel button is clicked

In this case, the action is opening a form.

I know how to get both buttons on the Message box, I just can't
figure out how to get them to do something other than allowing the
form to open

Thanks in advance to anyone who can help.
John

The buttons don't *do* anything but close the MsgBox. Your code that opened
the MsgBox has to examine the value returned by the MsgBox function and then
IT can decide what to do.

Assuming you are opening the MsgBox in the form's Open event...

If MsgBox("SomeText", vbOkCancel) = vbCancel Then
Cancel = True
End if

In the example it is the "Cancel = True" line that prevents the form from
opening.
 
G

Guest

John,

You have to use an if statement in your code:

If msgbox("Some message", vbOkCancel) = vbOK then
docmd.openform "formName"
else
'don't do anything if cancel is clicked
endif

HTH
Dale
 
G

Guest

Private Sub ButtonName_Click()
Dim stDocName As String
Dim stLinkCriteria As String
msg = "Select OK to open the form or Cancel"
style = vbYesNo
title = "Open form ?"
responce = MsgBox(msg, style, title)
If responce = vbYes Then
stDocName = "FormName"
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
End Sub


Or (with no criteria)

Private Sub ButtonName_Click()
msg = "Select OK to open the form or Cancel"
style = vbYesNo
title = "Open form ?"
responce = MsgBox(msg, style, title)
If responce = vbYes Then
DoCmd.OpenForm "FormName", acNormal, "", "", , acNormal
End If
End Sub


Change FormName to the real name of the form you "may" want to open

Good luck
 

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