Help with arguments

Refer to Figure 3-5 (of the Help page for the MsgBox keyword) to see the Settings section (below the argument descriptions) that provides some specific info on using the buttons argument. You can use either the constant or the value in the command. For example, if you want the MsgBox statement to show both an OK and a Cancel button (rather than just an OK button), you would use either the value 1 or the constant vbOKCancel as the second argument in the MsgBox statement. Arguments are always separated by commas, so the correct syntax would be either x = MsgBox("Hello World",!)

or x = MsgBox("Hello World",vbOKCancel)

A constant is a special word in VBA that's been assigned some value that never changes. For example, the constant vbOKOnly is always the same as the value 0. You can use vbOKCancel (which is easier to remember) in place of 0 in a MsgBox statement.

As instructed in the Help page, you can combine values (by using a + sign) in the buttons argument to use multiple options. For example, the vbYesNo setting (value = 4) displays Yes and No buttons in the message box. The vbQuestion setting (value = 32) setting displays a question mark icon in the message box. Thus, if you want to display a message box that displays the question Are you there?, a question mark icon, and Yes and No buttons, you could type any of the following statements. (The 36 is allowed because the sum of the two settings' values, 4 and 32, equals 36.)

x

= MsgBox("Are

you

there?"

,vbQuestion+vbYesNo)

x

= MsgBox("Are

you

there?"

,32+4)

x

= MsgBox("Are

you

there?"

,36)

You can test out any of those VBA statements by typing it into the Immediate window and pressing Enter. Because all three statements product the same result, you'll see a message box with the prompt Are you there?, a question mark icon, and Yes and No buttons, as in Figure 3-7.

Figure 3-7:

Test a MsgBox statement in the Immediate window.

Figure 3-7:

Test a MsgBox statement in the Immediate window.

Statement typed into Immediate window Result of executing statement

The third optional argument in the MsgBox keyword, title, allows you to specify a title to display in the dialog box. If you omit that argument in Access, the default title for all Access message boxes — Microsoft Office Access — appears in the message box. If you include a title (as text in quotation marks), that title replaces the default title. For example, if you test this command x = MsgBox("Are you there?",vbQuestion+vbYesNo,"Howdy")

in the Immediate window, the message box opens with the word Howdy, rather than Microsoft Office Access, in its title bar.

The order of arguments in a VBA statement is critical. For example, the title for a MsgBox must be the third argument in the statement. If you want to use a title argument but not a buttons argument, you'd have to still include a placeholder comma for the buttons argument and a similar comma for the title argument, as in the following example:

x = MsgBox("Hello World", ,"Howdy")

In this statement, the first argument (prompt) is "Hello World", and the second argument — which acts as a placeholder for the buttons argument — is empty. Because you've omitted the argument, Access will use the default value for that argument, which is vbOKOnly (0). Thus, when the statement executes, the message box will appear with the default OK button only. The third argument is "Howdy", which will appear in the message box title bar.

0 0

Post a comment