HandsOn Formatting the Message

1. Enter the following instruction in the Immediate window. Make sure to enter the entire text string on one line, and then press Enter.

MsgBox "All done. Now open ""Test.doc"" and place an empty disk in the diskette drive. The following procedure will copy this file to the disk."

As soon as you press Enter, Visual Basic shows the resulting dialog box. If you get the Compile error, click OK. Then, make sure that the name of the file is surrounded by double quotation marks — ""Test.doc"".

Figure 4-2: This long message will look more appealing to the user when you take the text formatting into your own hands.

Figure 4-2: This long message will look more appealing to the user when you take the text formatting into your own hands.

When the text of your message is particularly long, you can break it into several lines using the VBA Chr function. The Chr function requires a number from 0 to 255, and returns a character represented by this number. For example, Chr(13) returns a carriage return character (this is the same as pressing the Enter key), and Chr(10) returns a linefeed character (this is useful for adding spacing between the text lines).

Introduction to Access 2003 VBA Programming

2. Modify the instruction entered in the previous step in the following way:

MsgBox "All done." & Chr(13) & "Now open ""Test.doc"" and place" & Chr(13) & "an empty disk in the diskette drive." & Chr(13) & "The following procedure will copy this file to the disk."

Figure 4-3: You can break a long text string into several lines by using the Chr(13) function.

You must surround each text fragment by quotation marks. Quoted text embedded in a text string requires an additional set of quotation marks, as in ""Test.doc"". The Chr(13) function indicates a place where you'd like to start a new line. The string concatenation character & is used to add a carriage return character to a concatenated string. When you enter exceptionally long text messages on one line, it's easy to make a mistake. An underscore (_) is a special line continuation character in VBA that allows you to break a long VBA statement into several lines. Unfortunately, the line continuation character cannot be used in the Immediate window.

3. Now add a new module by choosing Insert | Module.

4. In the Code window, enter the MyMessage subroutine as shown below. Make sure to precede each line continuation character with a space.

Sub MyMessage()

MsgBox "All done." & Chr(13) _ & "Now open ""Test.doc"" and place" & Chr(13) & "an empty disk in the diskette drive." & Chr(13) & "The following procedure will copy this file to the disk." End Sub

When you run the MyMessage procedure, Visual Basic displays the same message as the one illustrated in Figure 4-3. As you can see, the text entered on several lines is more readable, and the code is easier to maintain. To improve the readability of your message, you may want to add more spacing between the text lines by including blank lines. To do this, use two Chr(13) or two Chr(10) functions, as shown in the following step.

5. Enter the following MyMessage2 procedure:

Sub MyMessage2()

MsgBox "All done." & Chr(10) & Chr(10) _ & "Now open ""Test.doc"" and place" & Chr(13) _ & "an empty disk in the diskette drive." & Chr(13) & Chr(13) _ & "The following procedure will copy this file to the disk." End Sub

Part I

Figure 4-4: You can increase the readability of your message by increasing spacing between selected text lines.

Now that you have mastered the text formatting techniques, let's take a closer look at the next argument of the MsgBox function. Although the buttons argument is optional, it's frequently used. The buttons argument specifies how many and what types of buttons you want to appear in the message box. This argument can be a constant (see Table 4-1) or a number. If you omit this argument, the resulting message box includes only the OK button, as you've seen in the preceding examples.

Table 4-1: The MsgBox buttons argument's settings




Button settings



Displays only an OK button. This is the default.



OK and Cancel buttons



Abort, Retry, and Ignore buttons



Yes, No, and Cancel buttons



Yes and No buttons



Retry and Cancel buttons

Icon settings



Displays the Critical Message icon



Displays the Question Message icon



Displays the Warning Message icon



Displays the Information Message icon

Default button settings



The first button is default



The second button is default



The third button is default



The fourth button is default

Message box modality



The user must respond to the message before continuing to work in the current application.



On Win16 systems, this constant is used to prevent the user from interacting with any other window until he or she dismisses the message box. On Win32 systems, this constant works like the vbApplicationModal constant (see above) with the following exception: The message box always remains on top of any other programs you may have running.

Other MsgBox display settings



Adds the Help button to the message box



Specifies the message box window as the foreground window

Passing Arguments to Procedures and Functions | 63 Introduction to Access 2003 VBA Programming






Text is right aligned



Text appears as right-to-left reading on Hebrew and Arabic systems

When should you use the buttons argument? Suppose you want the user of your procedure to respond to a question with Yes or No. Your message box may then require two buttons. If a message box includes more than one button, one of them is considered a default button. When the user presses Enter, the default button is selected automatically.

Because you can display various types of messages (critical, warning, information), you can visually indicate the importance of the message by including in the buttons argument the graphical representation (icon) for the chosen message type. In addition to the type of message, the buttons argument can include a setting to determine if the message box must be closed before the user switches to another application. It's quite possible that the user may want to switch to another program or perform another task before he responds to the question posed in your message box. If the message box is application modal (vbApplication Modal), then the user must close the message box before continuing to use your application.

For example, consider the following message box:

MsgBox "How are you?", vbOKOnly + vbApplicationModal, "Application Modal"

If you type the above message box in the Immediate window and press Enter, a message box will pop up and you won't be able to work with your currently open Microsoft Office Access application until you respond to the message box.

On the other hand, if you want to keep the message box visible while the user works with other open applications, you must include the vbSystemModal setting in the buttons argument, like this:

MsgBox "How are you?", vbOKOnly + vbSystemModal, "System Modal"

^^ Note: Use the vbSystemModal constant when you want to ensure that your message box is always visible (not hidden behind other windows).

The buttons argument settings are divided into five groups: button settings, icon settings, default button settings, message box modality, and other MsgBox display settings (see Table 4-1). Only one setting from each group can be included in the buttons argument. To create a buttons argument you can add up the values for each setting you want to include. For example, to display a message box with two buttons (Yes and No), the question mark icon, and the No button as the default button, look up the corresponding values in Table 4-1, and add them up. You should arrive at 292 (4 + 32 + 256).

To see the message box using the calculated message box argument, enter the following statement in the Immediate window:

MsgBox "Do you want to proceed?", 292

Part I

The resulting message box is shown in Figure 4-5.

Figure 4-5: You can specify the number of buttons to include in the message box by using the optional buttons argument.

When you derive the buttons argument by adding up the constant values, your procedure becomes less readable. There's no reference table where you can check the hidden meaning of 292. To improve the readability of your MsgBox function, it's better to use the constants instead of their values. For example, enter the following revised statement in the Immediate window:

MsgBox "Do you want to proceed?", vbYesNo + vbQuestion + vbDefaultButton2

The above statement produces the same result as shown in Figure 4-5. The following example shows how to use the buttons argument inside the Visual Basic procedure.

0 0

Post a comment