In your programs and macros, you can use message boxes to display information or ask for confirmation of a task. By using an If structure or Select Case structure linked to the value that the message box returns, you can determine which button In the . message box the user has chosen. You can use this value to direct the program flow appropriately.
The syntax for displaying a message box with VBA is as follows:
Prompt is the only required argument for the Msg Box statement. Prompt is a string that is displayed as the text in the message box.
Buttons is an optional argument that specifies the buttons that the message box displays, the icons that the message box displays, the default button, and the morality of the message box. Each of these four elements can be specified with either a value or a constant. You use the + operator to join the values or constants together.
Next, you can choose which (if any) of the standard four message box icons to display. The choices are shown in Table 24.4.
Next, you can set a default button for the message box. If you choose not to set a default button, VBA makes the first (leftmost) button in the message box the default the Yes button in a vb Yes No message box, OK button in a vb OK Cancel message box and so on. Table 24.S gives you the chortle Sot default buttons for a message box.
In theory, you can choose whether to make the message box application modal (the default) or system modal. Application modality means that you can take no further actions in the application until you dismiss the message box; system modality means you can take no further actions on your computer until you dismiss the message box. In practice, this system modality does not work-seven if you use the vb System Modal argument, the resulting message box will be application modal. (See Table 24.6.)
Finally, you can now specify some new information for your message box. The Help button can now be automatically added, the message box window can be set in the foreground, and two options that are most useful when working with Project in a language that reads from right to left. Text can be right-aligned, and text can read from right to left. (See Table 24.7.)
Title is an optional string argument specifying the text to appear in the title bar of the message box. In most cases, it’s a good idea to specify a Title argument to make it clear which procedure has caused the message box to appear. If you do not specify a Title argument for the message box, VB Awill display Visual Basic in the title bar, which is singularly uninformative
using Input Boxes
You can use input boxes to gather single pieces of information from the user. By using an If structure linked to the value the input box returns. you can determine whether the user has entered valid information and direct program flow appropriately. The syntax for an input box is as follows
title is an optional string argument that specifies the text for the title bar of the input box. As with Msg Box.if yOU don’t specify a Title argument, VB Awill display Visual Basic in the title bar of the input box. is an optional string argument that specifies the default text to display in the text box inside the input box, Often, you’ll want to omit the Default argument other times, you may want to use a Default argument to provide text likely to be suitable for the user (for example, the current user name in an input box asking for the user’s name) or text that may need adjustment,
You can then check the value of the variable (here, str User Name) to make sure that it is not an empty string (‘ ‘), which will result if the user either clicks the Cancel button in the input DOx or clicks the OK button without entering any text in the text box. Figure 24.8 shows this input box
This chapter introduced you to essential VBA concepts so you can begin writing your own VBA code. In the next chapter, you’ll apply the information from this chapter to create a custom VBA application in Project 2000