Getting keyword help

Whether you're typing your own code or trying to modify someone else's, you can get information on any keyword at any time. Just select (doubleclick) the keyword right in the Code window where it's typed. Then press the Help key (F1) on your keyboard. The Help window that opens describes the command and its syntax.

After you type a keyword into a procedure, it's very easy to get more detailed help. Just select (double-click) the keyword, right where you typed it, and press the Help key (F1). This is also a great way to learn more about code other people have written because you can learn what each line of code does by double-clicking the first word in the line and pressing F1.

When you press F1, the Help page that opens describes whatever keyword you selected in your module. For example, if you double-click MsgBox in a procedure (to select it) and then press F1, the Help page for the MsgBox keyword opens, as in the example shown in Figure 3-4.

Figure 3-4:

Help for the MsgBox keyword.

Selected keyword (MsgBox) Help for MsgBox keyword

Figure 3-4:

Help for the MsgBox keyword.

Selected keyword (MsgBox) Help for MsgBox keyword

The Help window shows a ton of information about using the MsgBox keyword. The first paragraph describes what the keyword does. Under the heading, the syntax chart shows the same information that the Quick Info does (namely, the arguments that you can use with the keyboard) as well as the order in which you must use them. For example, the syntax chart for MsgBox looks like this:

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The first word, MsgBox in this example, is the keyword. The text and symbols enclosed in parentheses represent arguments that you can use with the MsgBox keyword. An argument is a piece of information that you give to the keyword to use for something. (More on that in the upcoming section, "Help with arguments.") The syntax chart uses square brackets, boldface, and italics as follows:

^ Bold: Represents a required keyword.

^ Italic or bold italic: Represents an argument.

^ [ ]: Indicates that the argument is optional and can be omitted. Never type the square brackets into your code, or the code won't work.

Beneath the syntax chart is a description of each argument that the keyword supports. For example, scrolling down a short way through this Help page reveals a description of each of the argument names that MsgBox supports, as in Figure 3-5.

The description of an argument tells you whether the argument is required or optional. If an argument is required, you must type an acceptable value for that argument into your code (always within the parentheses that follow the keyword). If an argument is optional, you can either type in an acceptable value for the argument or just not use the argument at all.

Either way, remember that you never type square brackets into your VBA code: The square brackets in the syntax chart are just there to indicate the optional arguments.

The argument acts as a placeholder for some actual value that you'll later pass to the procedure. If you have any experience at all using Access expressions, you're familiar with arguments. For example, in the expression Sqr(81), 81 is the value being passed to the Sqr() (square root) function. When executed, the function returns 9 because 9 is the square root of 81.

Figure 3-5:

Find argument info in a keyword Help window.

»^Microsoft Visual Basic Help !-llfi;[x]

□ ê p. s

more than one line., you can separate the lines using a carriage return character (Chr(13)), a linefeed character (Chr(10))j or carriage return - linefeed character combination (Chr(13) & Chr(lO)) between each line.



Optional, Numeric expression that is the sum of values soecifvina the number and tvoe of buttons to display; the icon style to use, the identity of the default button, and the modality of the message box, If omitted, the default value for buttons is 0,


Optional. String expression displayed in the title bar of the dialog box. If you omit iitfe, the application name is placed in the title bar.


Optional. String expression that identifies the Help file to use to provide context-sensitive Help for the dialog box. If heipfile is provided; context must also be provided.



Optional. Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If context is provided , hetpfiie must also be provided.


The buttons araurn

ent settings are:


Value Description


0 Display OK button only,


1 Display OK and Cancel buttons.

vbAbortRetryIgnore 2 Display Aborts Retry, and Ignore buttons.


3 Display Yes, No, and Cancel buttons.


4 Display Yes and No buttons.


5 Display Retry and Cancel buttons,


16 Display Critical Message icon,


32 Display Warning Query icon.


48 Display Warning Message icon,

What constitutes an acceptable value for an argument is usually the second thing listed in the Help chart. Typically, it will be one of the following:

l String expression: This can be literal text enclosed in quotation marks, as in "Hello World", or an expression that results in text.

l Numeric expression: This can be a number like 1 or 10, or it can be an expression that results in a number.

That's a lot to try to understand. Take it one step at a time, though, with an example to try to make sense of it all. First, understand that the arguments of a keyword are always typed within parentheses, after the keyword. And multiple arguments are always separated by commas. So the most general view of any keyword that accepts three arguments would be as follows:

keyword (argument1, argument2, arguments)

In other words, you don't start typing the first argument until you've typed in the keyword and opening parenthesis. After you type the first argument, you have to type a comma before you start typing the second argument and so forth. The VBA editor won't know that you're ready to type the next argument until you type that comma. And finally, you have to type the closing parenthesis at the end of the statement. If you mess it up, you'll get a compile error as soon as you press the Enter key. All you can do is click OK and try again. (Or delete the whole line.)

Getting back to the MsgBox() keyword and its arguments, you can see at the top of the first Help page (refer to Figure 3-4) that MsgBox() is actually a function that returns a value. Although it's not specifically stated in the syntax, it means that to use the command properly in a procedure, you need to use this syntax:

x = Msgbox(prompt[, buttons ][, title][,helpfile, context])

You can see in the Help page that the prompt argument is required and must be a string expression. So if you want the message box to display Hello World, you would type those words (remembering to enclose them in quotation marks) as the first argument, as in the following example. Because the remaining arguments are optional, you could omit them and just end the whole line with a closing parenthesis, as follows:

x = MsgBox("Hello World")

The Immediate window that I discuss in Chapter 2 provides a handy means of testing a VBA statement on-the-fly to see whether it will work when actually executed in your code. For example, if you type (exactly) x=MsgBox("Hello World") into the Immediate window and press Enter, VBA will execute the statement. The result is a message box containing the words Hello World, as in Figure 3-6. (You have to click the OK button in the message box to get back to working in the Visual Basic editor.)

Figure 3-6:

Test VBA statements in the Immediate window.

Figure 3-6:

Test VBA statements in the Immediate window.

Statement entered in Immediate window

Result of executing statement

Statement entered in Immediate window

Result of executing statement

Was this article helpful?

0 0

Post a comment