vbOkCancel Msg will not cancel

J

Justin83716

How do I get the cancel button to cancel the action in error 2169 below? I
know I need to specify what action to take when cancel is selected, but how
do I phrase it? Currently pressing OK or Cancel both respond as though you
pressed OK.

Thanks!

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Enter data in all required fields."
ElseIf DataErr = 2169 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Close form without saving?", vbExclaimation + vbOKCancel
ElseIf DataErr = 2113 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Enter valid start date."
Else
MsgBox "Error#: " & DataErr 'Display the error number
Response = acDataErrDisplay 'Display default message

End If

End Sub
 
F

fredg

How do I get the cancel button to cancel the action in error 2169 below? I
know I need to specify what action to take when cancel is selected, but how
do I phrase it? Currently pressing OK or Cancel both respond as though you
pressed OK.

Thanks!

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Enter data in all required fields."
ElseIf DataErr = 2169 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Close form without saving?", vbExclaimation + vbOKCancel
ElseIf DataErr = 2113 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Enter valid start date."
Else
MsgBox "Error#: " & DataErr 'Display the error number
Response = acDataErrDisplay 'Display default message

End If

End Sub

No where in the code do you actually tell it to close the form.
...................
ElseIf DataErr = 2169 Then
Response = acDataErrContinue 'Don't display the default message
If MsgBox ("Close form without saving?", vbExclaimation +
vbOKCancel) = vbOK Then
DoCmd.Close acForm, Me.Name, acSaveNo
ElseIf etc ..............
 
M

Marshall Barton

Justin83716 said:
How do I get the cancel button to cancel the action in error 2169 below? I
know I need to specify what action to take when cancel is selected, but how
do I phrase it? Currently pressing OK or Cancel both respond as though you
pressed OK.

Thanks!

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 3314 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Enter data in all required fields."
ElseIf DataErr = 2169 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Close form without saving?", vbExclaimation + vbOKCancel
ElseIf DataErr = 2113 Then
Response = acDataErrContinue 'Don't display the default message
MsgBox "Enter valid start date."
Else
MsgBox "Error#: " & DataErr 'Display the error number
Response = acDataErrDisplay 'Display default message

End If

End Sub


You have to use the function syntax to call the MsgBox:

rsp = MsgBox("Enter data in all required fields.", _
vbOkCancel)
If rsp = vbCancel Then do something
 

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