msgbox open table even if cancel is clicked

R

Rowland

I have a message box that pops up, when an option is
choose. The message box should open a table when ok is
pressed, and should return to the option form when cancel
is pressed. However it opens the table no matter what
button is pressed. There is the code on the form

Private Sub cmd_ok_Click()

If (Me.opt_add_edit_menu.Value = 1 And opt_choices.Value
<> 5) Then
DoCmd.OpenForm "frm_search", acNormal
ElseIf (Me.opt_add_edit_menu.Value = 1 And
opt_choices.Value = 5) Then
MsgBox "This action will open the states table for
adding" & vbCrLf & "and/or editing and or deleting", _
vbOKCancel, "Are you sure?"
If vbOK = 1 Then
DoCmd.OpenTable "tbl_type_state", acViewNormal
DoCmd.Close acForm, "frm_add_edit_menu"
Else
Exit Sub
End If
ElseIf (Me.opt_add_edit_menu.Value = 2 And
opt_choices.Value = 5) Then
MsgBox "This action will open the states table for
adding" & vbCrLf & "and/or editing and/or deleting", _
vbOKCancel, "Are you sure?"
If (vbOK) Then
DoCmd.OpenTable "tbl_type_state", acViewNormal
DoCmd.Close acForm, "frm_add_edit_menu"
Else
Exit Sub
End If
End If
 
R

Rowland

nevermind, I figured it out, for those of you that may by
wondering the same thing, here is the code:



Dim msg
Dim prompt1 As String
Dim prompt2 As String
Dim prompt3 As String

prompt1 = "This action will open the states table for
adding"
prompt2 = " and/or editing and or deleting"
prompt3 = " Are you Sure?"

If (Me.opt_add_edit_menu.Value = 1 And opt_choices.Value
<> 5) Then
DoCmd.OpenForm "frm_search", acNormal
ElseIf (Me.opt_add_edit_menu.Value = 1 And
opt_choices.Value = 5) Then
GoTo cmd_ok_msg
ElseIf (Me.opt_add_edit_menu.Value = 2 And
opt_choices.Value = 5) Then
GoTo cmd_ok_msg
End If


-----Original Message-----
I have a message box that pops up, when an option is
choose. The message box should open a table when ok is
pressed, and should return to the option form when cancel
is pressed. However it opens the table no matter what
button is pressed. There is the code on the form

Private Sub cmd_ok_Click()

If (Me.opt_add_edit_menu.Value = 1 And opt_choices.Value
<> 5) Then
DoCmd.OpenForm "frm_search", acNormal
ElseIf (Me.opt_add_edit_menu.Value = 1 And
opt_choices.Value = 5) Then
MsgBox "This action will open the states table for
adding" & vbCrLf & "and/or editing and or deleting", _
vbOKCancel, "Are you sure?"
If vbOK = 1 Then
DoCmd.OpenTable "tbl_type_state", acViewNormal
DoCmd.Close acForm, "frm_add_edit_menu"
Else
Exit Sub
End If
ElseIf (Me.opt_add_edit_menu.Value = 2 And
opt_choices.Value = 5) Then
MsgBox "This action will open the states table for
adding" & vbCrLf & "and/or editing and/or deleting", _
vbOKCancel, "Are you sure?"
If (vbOK) Then
DoCmd.OpenTable "tbl_type_state", acViewNormal
DoCmd.Close acForm, "frm_add_edit_menu"
Else
Exit Sub
End If
End If
............................

cmd_ok_msg:
msg = MsgBox(prompt1 + vbCrLf + prompt2 + vbCrLf +
prompt3, vbYesNo, "Opening States Table")
If msg <> vbNo Then
DoCmd.OpenTable "tbl_type_state", acViewNormal
DoCmd.Close acForm, "frm_add_edit_menu"
Else
Exit Sub
End If
 
L

Larry R Harrison Jr

One other option you may be aware of is using a Y/N type
of message box. I use this one on an exit button which
asks the user if they are sure they want to exit:

If MsgBox("Quit? (Y/N)?", vbYesNo, "Quit?") = vbYes Then
DoCmd.Quit
End If

Yours could look something like this


If MsgBox("This will open the tables; are you sure?", _
vbYesNo, "Confirm Action") = vbYes Then
' Code for Opening Table
Else
' Code for exiting program
End if


Or ...

If MsgBox("This will open the tables; are you sure?", _
vbYesNo, "Confirm Action") = vbNo Then
End
End if
' Code for opening tables

LRH
 

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