Msgbox results ?

JOHNHO used Ask the Experts™
Whats wrong with the below code and pls give the correct code.

msgbox "Pls Chose",vbokcancel
if vbok then
msgbox "Ok"
msgbox "Cancel"
end if

it keep promting the ok after i press the msgbox .
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try it this wa:

Dim Answer as Integer

Answer = MsgBox("Pls Chose",vbokcancel)
if Answer = vbOK then
msgbox "Ok"
msgbox "Cancel"
end if

  Lookup MsgBox in the VB help system.  You will find that it can be used like a SUB AND like a Function (use it like a function when you want to make use of the returned value - which corresponds to the button that the user clicked on)

Arthur Wood

try the following :

dim message as string

message = "Pls Chose"

If MsgBox(message, vbOKCancel) = vbOK Then
   MsgBox "OK"
   MsgBox "Cancel"
end if

Good Luck


actually speaking message box is an API function called
Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

as you see the API function returns a long value denoting key pressed the user

you have to code like this

dim intKeyPressd as long
dim strMessage as string
strMessage = "Please Close"

intKeyPressed = msgbox(strMessage,VBInformation + VbOkCancel)

if intKeyPressed = Vbok then
<perform operations>
end if

actually the good practice in programming is to assign the result value in an long variable and assigning it to IF loop.

Even better is this (notice the dim type):

Dim mbxAnswer as VbMsgBoxResult

mbxAnswer = MsgBox("Pls Chose",vbokcancel)

select case mbxAnswer
  case vbOK
  case vbCancel
end select
Author of the Year 2009

It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Arthur_Wood@devx's comment(s) as an answer.

JOHNHO, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial