Message Boxes

ex121sol

You can add window's type message boxes to your applications to give them a more professional look.

For example the command MsgBox(prompt[,buttons][,title])

Exercise

We can create this macro by using the Visual Basic Editor

Select
Tools - Macro - Visual Basic Editor

Select Insert - Module

Now enter the following

sub dialogbox
response=MsgBox("I'm sorry I can't do that Dave",0,"")
end sub

Return to Excel Sheet 1

Run the macro using the Tools Macro options. You should have the following dialog box.

dialog1

The MsgBox function syntax has these named-argument parts:

The Prompt- ("I'm sorry I can't do that Dave")
String expression displayed as the message in the dialog box.

Buttons - (0)
Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality.

If omitted, the default value for buttons is 0.

The Buttons variable can have these values:

Number and Type of button

0 Display OK button only.
1 Display OK and Cancel buttons.
2 Display Abort, Retry, and Ignore buttons.
3 Display Yes, No, and Cancel buttons.
4 Display Yes and No buttons.
5 Display Retry and Cancel buttons.

Icons

16 Display Critical Message icon.
32 Display Warning Query icon.
48 Display Warning Message icon.
64 Display Information Message icon.

By adding together values for the Button and Icons we get a combined effect.
For example a value of 50 would give us a dialog box with Abort, Retry, and Ignore buttons and a Warning message Icon.

Title

This is the string expression displayed in the title bar of the dialog box.

If you omit the title, as in the above example, nothing is placed in the title bar.

Try adding the title "Hal 2001" to the previous example

sub dialogbox
response=MsgBox("I'm sorry I can't do that Dave",0,"Hal 2001")
end sub

Practice Exercises

You should attempt exercise 12.1, 12.2, and 12.3