What is the syntax to use message boxes?

MsgBox(prompt, buttons, title, helpfile, context)
‘prompt – message displayed in msgbox; mandatory parameter with max length 2014 characters
‘buttons – integer telling VBA which button options to show; optional parameter with default value 0

 

Which button options do I have?

  • 0 vbOKOnly Displays OK button only.
  • 1 vbOKCancel Displays OK and Cancel buttons.
  • 2 vbAbortRetryIgnore Displays Abort, Retry, and Ignore buttons.
  • 3 vbYesNoCancel Displays Yes, No, and Cancel buttons.
  • 4 vbYesNo Displays Yes and No buttons.
  • 5 vbRetryCancel Displays Retry and Cancel buttons.
  • 16 vbCritical Displays Critical Message icon.
  • 32 vbQuestion Displays Warning Query icon.
  • 48 vbExclamation Displays Warning Message icon.
  • 64 vbInformation Displays Information Message icon.
  • 0 vbDefaultButton1 First button is default.
  • 256 vbDefaultButton2 Second button is default.
  • 512 vbDefaultButton3 Third button is default.
  • 768 vbDefaultButton4 Fourth button is default.
  • 0 vbApplicationModal Application modal. The current application will not work until the user responds to the message box.
  • 4096 vbSystemModal System modal. All applications will not work until the user responds to the message box.

 

What are the integer return values that identify which buttons were clicked?

  • 1 – vbOK – OK was clicked
  • 2 – vbCancel – Cancel was clicked
  • 3 – vbAbort – Abort was clicked
  • 4 – vbRetry – Retry was clicked
  • 5 – vbIgnore – Ignore was clicked
  • 6 – vbYes – Yes was clicked
  • 7 – vbNo – No was clicked

 

‘example
Dim userReturn As Integer

userReturn = MsgBox("Do you agree?", 4 + 64 + 256, "Choice")
If userReturn = 6 Then
    MsgBox ("You just agreed.")
Else
    MsgBox ("You do not agree.")
End If

 

 

Advertisements