To use a message box, you will need to know how it all works, how everything is put together. Message boxes are useful and are needed for things like if the program asks the user for a Question, the program delivers a Error Message and wants the user to know, expressing certain Conditions and much more.
Basically, you need to follow this syntax:
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
If you want to call on the message box to do something, this must be used:
memory_variable = MsgBox ( prompt [, icons+ buttons] [,title] )
Each part of the Syntax is explained:
This is required as it is a string expression displayed as the message in the dialog box. This could be a warning, question or just a message for users to know.
This is an optional thing to include with your message box. Basically, this is a numeric expression that is the sum of values specifying the number and type of button/s to display. As this is optional, if this is omitted, it is set to its default (0) which causes only an OK button to be displayed. You may use either the worded syntax, or the assigned number.
This is the title of the message box which is going to be displayed. This is optional and if omitted, a default name will be used.
helpfile and context
These arguments are only applicable when a Help file has been set up to work with the application and therefore these are optional.
- - - - - - -
A simple example to use is:
MsgBox "Hello there!"
When this is used under the Form_Load event, the most basic of all message boxes will appear, expressing the message "Hello there!".
We can expand on our first example and use the memory_variable:
Dim Response As Integer Response = MsgBox("Is this a great tutorial?", vbYesNo) If Response = vbYes Then MsgBox("Thank You!") Else MsgBox("ORLYNAO?") End If
This would be a message box appearing, with a Yes and No buttons and if the user selects the first button, Yes, another message box will appear saying "Thank you!". If the second button is clicked, a message box will appear saying "ORLYNAO?".
Obviously, we can expand even more. We can add features and extra emphasis on the message. We can add a button like so:
MsgBox "The floor is wet!", vbExclamation + vbOKOnly, "Caution"
Simply, an Exclamation icon will appear. The vbExclamation can be replaced with other variables, listed below.
And we can make a button a default when the message box pops up:
Dim Response As Integer Response = MsgBox("Do you really like this tutorial?", vbYesNo + vbQuestion + vbDefaultButton1, "Question?") If Response = vbYes Then MsgBox “YAY!!!” Else MsgBox “Mhmmm...” End If
The first button will be the default in this can. Like before, it can be changed.
- - - - - - -
Attached - Table of buttons and icons:
This post has been edited by firebolt: 14 August 2009 - 11:22 PM