Message Boxes

BoroLee

Registered User.
Local time
Today, 05:46
Joined
Aug 30, 2000
Messages
90
I have created a message box using code with OK and Cancel boxes. Why, when I click Cancel button does the code continue to run ???
 
You need to tell Access what do to with the cancel like this:

If Answer = vbCancel Then Exit Sub
Cancel = True

Here is the whole Sub if you want to see how I did it in my app. I have been told that the way I do this is not efficient but it works well for me and I have never had any errors with the procedure.

Dim strMsg As String, strTitle As String
Dim intStyle As Integer
Dim Answer As Variant

If DCount("[jobname]", "JobNumbers", "[JobNumber]= '" _
& Me!JobNumber & "'") = 0 Then

strMsg = "Please try a different number"
strTitle = "Invalid Job Number"
intStyle = vbOKCancel + vbInformation
Answer = MsgBox(strMsg, intStyle, strTitle)

If Answer = vbCancel Then Exit Sub
Cancel = True
End If
 
Merely setting CANCEL=True does not stop code from running.
 
To follow on from Talismanic's comments you could further enhance this to set the focus to a particular control (assuming your message box is on a form)

However if you are relying on the Cancel = True to prevent the BeforeUpdate_Event from continueing then you need to set Cancel = True before you exit the sub

e.g
If Answer = vbCancel Then
Cancel = True
myControl.SetFocus
Exit Sub
Else
your code to process vbOK
End If

HTH
Rob
 

Users who are viewing this thread

Back
Top Bottom