Can't use second parameter in MsgBox

  • Thread starter Thread starter Laurel
  • Start date Start date
L

Laurel

When I use MsgBox in my script, if I add the parameter for the type of box
in the second parameter, I get a syntax error. I choose the parameter from
the list of available ones that appears as soon as I enter the comma. So
I'm not mis-typing. I've tried this with several different button types -
vbOKCancel, vbCritical and others. The minute I choose the second
parameter, the statement goes red and a compile gives me a syntax error. I
have executed "compact and repair" several times. Any ideas what might be
wrong?
 
Do you mean you want to combine vbYesNo with vbCritical?

Are you typing a plus sign between the two constants? You need to.

MsgBox "This is my text!", vbOkCancel + vbCritical

If that is not the problem, perhaps you could post what you have tried and
is failing. Just copy and paste into your message.
 
you need to add them together, not separate them with commas.

vbYesNo + vbCritical + dbDefaultButton2

The values for these contstants are set up so that no combination of the
available options results in the same number.
 
No, I mean I can't use any value at all from the list in the second
parameter. I've tried vbOKCancel and I've tried, separately, vbCritical.
That was just an example of several I tried.
 
I haven't been clear. I don't want to combine anything. I just can't put
ANY legitimate value for the button types in the second parameter. I get a
syntax error. I choose the item from the list that VB shows me, so I'm not
typing it wrong.
 
That is strange.

Apologies in advance if I'm coming across as if you are a beginner and you
are not one.

What version of Access?

Please post the exact line of code that you are attempting to use.

For instance, of the following three lines, the last two will generate an
error.
MsgBox "This is my text!", vbOkCancel
MsgBox ("This is my text",vbOkCancel)
X = MsgBox "This is my text!", vbOkCancel

Also, you could try using the actual value of vbOkCancel (1). Just to see
if that works.
MsgBox "This is my text!", 1
 
Well, I'm a semi-beginner. I looked through lots of code and couldn't
believe I'd never used a message box with more than once choice of response.
I was using your form 2. I had parentheses around all my other uses of
msgbox parameters, but I only ever had the text - nothing else. Again, I
couldn't believe it. But I don't understand the principle here. I use
parentheses around multiple params for functions I've written myself.
What's the rule?

Thanks very much!

Oh. I see I could also have fixed it by doing this gi_variant = MsgBox ("my
prompt", vbOKCancel)

I think I'm beginning to see a pattern... of sorts.
 
The rule is if you are returning a value from a function (not a sub) then
you use the parentheses around the argument and if you use the parentheses
you must assign the value to something (or at least use the value).

The following are all valid ways to use the MsgBox function

Call MsgBox ("Test",vbOK) 'Ignores the return value
MsgBox "Test",vbOK 'Ignores the return value
X= MsgBox("Test",vbOK) 'Sets the value of X to the return value

'Check the return value against the value of vbOk
If MsgBox("Test",vbOK) = vbOk Then
'Do something here
End if
 
Thanks. Very helpful.
VB is not helpful in allowing parentheses around a single parameter, but no
return value and no Call. In other words, this is what I've been doing
forever, for simple OK boxes.

MsgBox("Test")

According to the rules, this should give me a syntax error. OH! Maybe it's
not the number of parameters, but whether or not the code inside TRIES to
return a value??? As it wouldn't with a simple vbOKOnly???

Oh well, not necessary to spend more time on this. Your explanation below
will be very helpful to me in the future in all sorts of situations, I'm
sure. Thanks again.
 
You've found an area in VBA that often causes confusion.

If there's only a single parameter being passed, you can put parentheses
around it, and that forces the parameter to be passed ByVal, not ByRef.

However, if you have more than one parameter, this doesn't work. What would
work would be:

MsgBox ("Test"), (vbOkOnly)

but not

MsgBox ("Test", vbOkOnly)
 
Well, thanks Doug. Just learned something new.


Douglas J. Steele said:
You've found an area in VBA that often causes confusion.

If there's only a single parameter being passed, you can put parentheses
around it, and that forces the parameter to be passed ByVal, not ByRef.

However, if you have more than one parameter, this doesn't work. What
would
work would be:

MsgBox ("Test"), (vbOkOnly)

but not

MsgBox ("Test", vbOkOnly)
 

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

Back
Top