MSGBOX Problems

J

James

Hello I have the following Code for an MSGBox and I am
getting an error or the Left Hand needs to contain an
Object or Varient???

I dont understand??

Private Sub Form_Click()
Dim strMsg As String
strMsg = "Please Press a Key to Continue!"
MsgBox(strMsg, vbOKOnly, "Error!") = vbOK
End Sub

Thanks

James
 
D

Douglas J. Steele

It's not possible to assign a value to the MsgBox function (i.e.: you can't
have the MsgBox function on the left hand side of an equal sign).

It's not clear from your code what you're trying to do. You're presenting
them with a message box that only has an OK button on it, and you seem to be
wanting to check whether they've clicked on the Ok button on the Message
Box. The Message Box is modal: nothing's going to happen until they click on
the button, and if the only button on the box is an OK button, why do you
need to check what button they clicked?
 
M

MikeB

James said:
Hello I have the following Code for an MSGBox and I am
getting an error or the Left Hand needs to contain an
Object or Varient???

I dont understand??

Private Sub Form_Click()
Dim strMsg As String
strMsg = "Please Press a Key to Continue!"
MsgBox(strMsg, vbOKOnly, "Error!") = vbOK

MsgBox has two (2) forms with slightly different syntax. The form you used in your code was the Function
format which was missing the Left Hand Operand.

MsgBox Method
strMsg = "Please Press a Key to Continue!"
MsgBox strMsg, vbOKOnly, "Error!"

MsgBox Function
dim iResult

strMsg = "Please Press a Key to Continue!"
iResult = MsgBox(strMsg, vbOKOnly, "Error!") = vbOK

if iResult = vbOk then
'Do This
else
'Do That
end if

Or:

if MsgBox(strMsg, vbOKOnly, "Error!") = vbOK then
'Do This
else
'Do That
end if



End Sub
 
D

Douglas J. Steele

I don't think your first example is going to work,Mike.

You've got the following statements in your code:

iResult = MsgBox(strMsg, vbOKOnly, "Error!") = vbOK

if iResult = vbOk then

The first one is going to evaluate to a boolean True or False (-1 or 0), so
iResult will never equal vbOk (which is equal to 1)

You need to lose the = vbOk at the end of the first line above.


--
Doug Steele, Microsoft Access MVP

(No private e-mails, please)



MikeB said:
MsgBox has two (2) forms with slightly different syntax. The form you
used in your code was the Function
 
M

MikeB

Douglas J. Steele said:
I don't think your first example is going to work,Mike.

You've got the following statements in your code:

iResult = MsgBox(strMsg, vbOKOnly, "Error!") = vbOK

Damn cut & Paste ;-). Thanks Doug. The OP will be happy!
 
G

Graham R Seach

James,

You can't assign a value to a messagebox, you can only return a value from
it:
If MsgBox(strMsg, vbOKOnly, "Error!") = vbOK Then

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
J

James

Ok then Well if I do the following code:

Private Sub Form_Click()
Dim strMsg As String
strMsg = "Please Press a Key to Continue!"
MsgBox(strMsg, vbOKOnly, "Error!")
End Sub

Then it comes up and says expects = .... Is there any way
so that I can just have this pop up then they click ok and
it goes away so its simple... nothing complex??

Many Thanks

James
 
G

Graham R Seach

James,

Sure, just lose the brackets.
MsgBox strMsg, vbOKOnly, "Error!"

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
J

James

Ok I have done this... and when I click on the form it
does nothing it just sits there... Is there anything I am
doing wrong?

Many Thanks

James
 
G

Graham R Seach

James,

Don't know - show me exactly what you have.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

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

Similar Threads


Top