MsgBox Cancel Button

CanWest

Registered User.
Local time
Today, 11:16
Joined
Sep 15, 2006
Messages
272
I am trying to get a basic msgbox to have a cancel button as well as an ok button. The purpose is to do a little validation in the Before Update event. I have tried these two examples

If Len(Me.Counsellor & vbNullString) = 0 Then
MsgBox "The Employer Liason Field can not be left blank."
Cancel = True
Me.Counsellor.SetFocus
Cancel = True
End If

If Nz(Counsellor, "") = "" Then
Cancel = True 'if necessary
MsgBox ("The Employer Liason field can not be left blank.")
Counsellor.SetFocus
End If

Does anyone have any suggestions.....
 
Last edited:
why not

msgbox("yourmessage",vbcancel+vbok,"Title")

would that help you out?
 
Actually that would be

If MsgBox("Your Message here", vbOkCancel, "Title") = vbCancel Then ...etc.
 
why not

msgbox("yourmessage",vbcancel+vbok,"Title")

would that help you out?

I Tried that and got the same error I got in my examples. The error is Compile Error: Expected =
 
Actually that would be

If MsgBox("Your Message here", vbOkCancel, "Title") = vbCancel Then ...etc.

Hi Bob

Got the same error as well. I have no idea what I am doing wrong. This is baffling me.
 
Hi Bob

Got the same error as well. I have no idea what I am doing wrong. This is baffling me.

Post the EXACT code you are trying (again, with the substitution where you used my suggestion).
 
I have used this to good effect, similar to boblarson

If MsgBox("stuff.....................................................................", _
vbQuestion + vbOKCancel, _
"Title stuff! . . .") = vbCancel Then ....
 
If Len(Me.Counsellor & vbNullString) = 0 Then <<<<--this really have &vbnullstring?
MsgBox "The Employer Liason Field can not be left blank."
Cancel = True
Me.Counsellor.SetFocus
Cancel = True
End If

If Nz(Counsellor, "") = "" Then
Cancel = True 'if necessary
MsgBox ("The Employer Liason field can not be left blank.")
Counsellor.SetFocus
End If
----------------------------------

where does the error occur?
 
Also have used this which I have retro fitted to various tasks, hope I'm being helpful here

Dim CBx As ComboBox, DL As String

Set CBx = Me![ComboboxName]
DL = vbNewLine & vbNewLine

If CBx.ListIndex = -1 Then 'No selection made!!
MsgBox "'ComboboxName' is a required field!" & DL & _
"You'll have to go back and make a selection ...", _
vbInformation + vbOKOnly, _
"Missing Data Error! . . ."
CBx.SetFocus
Cancel = True
End If

Set CBx = Nothing
 
Bob's code is correct, hence the error must be coming from somewhere else. As Bob said, we need to see the code for the entire event where you have the code placed.
 
Post the EXACT code you are trying (again, with the substitution where you used my suggestion).

Ok here goes. O btw the error happenes as I writing the code, not on execution

Below please find the modified code for one section

Code:
If Nz(EmployerLiaison, "") = "" Then
  Cancel = True 
  MsgBox("The Employer Liaison Field can not be left blank.", vbOkCancel, "Feild Can Not Be Left Blank")
  Counsellor.SetFocus
End If


Below is the code for the entire before update event

Code:
If Nz(EmployerLiaison, "") = "" Then
  Cancel = True 
  MsgBox ("The Employer Liason field can not be left blank. (Chr(13))Click cancel to return to the field to correct this.")
  Counsellor.SetFocus
End If

If Nz(Status, "") = "" Then
   MsgBox ("The Status field can not be left blank. Click cancel to return to the field to correct this.")
   Cancel = True 
   Status.SetFocus
End If

If Nz(GeographicArea, "") = "" Then
   MsgBox ("The Geographic Area field can not be left blank. Click cancel to return to the field to correct this.")
   Cancel = True
   GeographicArea.SetFocus
End If

If Nz(ServiceContract, "") = "" Then
   MsgBox ("The Service Contract field can not be left blank. Click cancel to return to the field to correct this.")
   Cancel = True 
   ServiceContract.SetFocus
End If

If Nz(EducationLevel, "") = "" Then
   MsgBox ("The Education Level field can not be left blank. Click cancel to return to the field to correct this.")
   Cancel = True 
   EducationLevel.SetFocus
End If

Thanks
 
Get rid of the parens for the MsgBox code. Since you aren't using the way I had, using the
If Msgbox( xxx.....etc.) Then

and you are using the message box by itself, you don't include parens:

MsgBox "The Education Level field can not be left blank. Click cancel to return to the field to correct this."

That goes for all of the places you are using the MsgBox code.

If it isn't preceded by an

If MsgBox

or

Something = MsgBox

then you don't use the parens.
 
if you use brackets with the additional parameters you HAVE to use

call msgbox syntax

call MsgBox("The Employer Liaison Field can not be left blank.", vbOkCancel, "Feild Can Not Be Left Blank")

you can say

msgbox "test", flags, "title"
msgbox "test"
msgbox ("test")


but if you have more than 1 arg, you cannot say
msgbox ("test", flags, "title")

you have to say
CALL msgbox ("test", flags, "title")


note it ihas been pointed out here that calling subs/functions with call, and without call ACTUALLY do slightly different things, altohugh I don't think it has any effect on a msgbox
 
Get rid of the parens for the MsgBox code. Since you aren't using the way I had, using the
If Msgbox( xxx.....etc.) Then

and you are using the message box by itself, you don't include parens:

MsgBox "The Education Level field can not be left blank. Click cancel to return to the field to correct this."

Thanks Bob. That worked perfectly, almost. The code now works with out getting the error. The reason for the cancel button is so that the user may return to the form to correct the problem. What actually happens though is the form continues to close. I thought the cancel button would stop the close process. What am I missing here..
 
if you use brackets with the additional parameters you HAVE to use

call msgbox syntax

call MsgBox("The Employer Liaison Field can not be left blank.", vbOkCancel, "Feild Can Not Be Left Blank")

you can say

msgbox "test", flags, "title"
msgbox "test"
msgbox ("test")


but if you have more than 1 arg, you cannot say
msgbox ("test", flags, "title")

you have to say
CALL msgbox ("test", flags, "title")


note it ihas been pointed out here that calling subs/functions with call, and without call ACTUALLY do slightly different things, altohugh I don't think it has any effect on a msgbox

Thank you kindly. I am relatively new to this vb thing. I used to do everything with macros. This has been quite the learning curve. These forums have been my most valuable resource..
 
Thanks Bob. That worked perfectly, almost. The code now works with out getting the error. The reason for the cancel button is so that the user may return to the form to correct the problem. What actually happens though is the form continues to close. I thought the cancel button would stop the close process. What am I missing here..

What you may need to do is to, at the top of the form module, put
Code:
Private blnNoClose As Boolean

And then in the Form's Unload event put

Code:
Cancel = blnNoClose

and then in your Before Update event, put
Code:
blnNoClose = Cancel
at the end of the Before Update sub.

that way, it will pass the correct value to the unload event if you have set Cancel = true for the update it will also send Cancel=True for the Unload Event which will cancel the form's closing.
 

Users who are viewing this thread

Back
Top Bottom