Setting the Tab Order for Controls (VBA Programming Concepts) (AutoCAD VBA)

When your application is running, you can tab from text box to text box. The tab order is the same as the order in which you added the controls. This section describes how you can change this order—especially if you want to add more controls as an afterthought.

If you have ever had a broken mouse, you may have resorted to pressing the Tab key to move around among controls. The following steps show you how to set the tabs in your application using the TabIndex property, so that users can tab through your controls in a logical order.

1.    Start a new application and insert a UserForm containing two text boxes and a command button.

2.    Choose Run ^ Sub/UserForm. UserForm1 opens with the Enter cursor positioned inside the text box at the top.

3.    Press the Tab key. The focus is moved to the next control in the Tab sequence, which is the second text box because it was placed on the UserForm after the first text box. By default, the Tab sequence follows the order in which the controls were placed on the UserForm.

4.    Press Tab again. A dotted rectangle appears on the command button to show that it now has the focus. If you press Enter at this moment, the application will react as if you had clicked this command button and will execute the associated event.

tmp757e197_thumb


Using the Shift+Tab key-combination allows you to go through the Tab sequence backwards.

5. Click the Close button to stop the application.

6. Press Tab again; this still works even though the application isn’t running. This time, the control with the focus is displayed just like the active control is dis-played—with a thick border and eight handles, as shown here:

tmp757e198_thumb

The order of the controls in the Tab sequence is initially determined by the chronological order in which controls were placed on the UserForm. If you are in Design mode, the Tab key visits all the controls in the sequence as you tab along. But when an application is actually running, only the controls with which the user interacts are visited by tabbing.

Suppose I need to add another text box between the two that are there already. By default, the Tab key will not visit this new, third text box until it has visited the command button. To change this arrangement, you set the TabIndex property in the Properties Window or in the code. The TabIndex property of each control is assigned a number that starts with zero, which represents the control’s position in the Tab sequence. If you adjust the TabIndex number of one of the controls, it is slotted into that position in the sequence, and the TabIndex properties of all the other controls are automatically updated accordingly.

Functions

A function is a special kind of procedure that returns a single value, by assigning it to the function’s name in the statement immediately before the End Function statement at the close of the function. You can append a type for this value to the end of the function’s opening statement, as follows:

tmp757e199_thumb

If the type is omitted, Visual Basic applies the Variant type.

Parameters and Arguments

A parameter is defined in the declaration of a function or procedure and is assigned a value by the calling statement, which is called an argument.

Parameters

Parameters make a procedure more generic, so it’s quite common to have a list of parameters specified in parentheses as the first line (which is where the procedure is declared). Commas separate the parameters in the list, as shown here:

tmp757e200_thumb

In the procedure call, arguments are used to pass values to these parameters, matching the values with parameters according to their order. For example, the procedure call for the ProcessTwoValues procedure could be as follows:

tmp757e201_thumb

where the first argument 10 is passed (or assigned) to the first parameter Valuel, and the second argument 6 is passed (or assigned) to Value2.

Arguments

When you call a procedure that has parameters, you allocate values in the way of arguments. These arguments are matched up with the procedure’s parameters, and the procedure executes. A procedure can be called in two ways:

tmp757e202_thumb

Both ways use the comma character (,) to separate the arguments. If you use the Call statement method, you must enclose the arguments in parentheses.

Programs

A program is a sequence of instructions that can be executed to perform some task. The term program is fairly generic and can encompass macros, procedures, and functions. The sequence of instructions can be in a high-level language such as Visual Basic, or in some other language that can be executed by a computer.

Applications

An application is a program developed to simplify a specific task such as word processing, spreadsheet analysis, or computer-aided drafting. The application is typically much larger than a single procedure, with plenty of facilities to perform various related things; the application seldom does just one particular thing.

An application normally has a GUI so that the user can interactively select the features to run. The act of selecting a feature causes one of the application’s event procedures to run, which in turn might call a function, a macro, or a procedure that would carry out the user’s requests.

Summary

After you’ve finished this topic, you’ll know the following about working with AutoCAD VBA:

•    The difference between statements and expressions.

•    How to declare variables.

•    How to use the Option Explicit statement.

•    How to handle arrays of variables.

•    All about constants and how to view them in the Object Browser.

•    Why you should use constants whenever possible.

•    The different levels of scope for constants and variables.

•    How to define your own types.

•    The different flavors of If statements.

•    How Select Case statements work.

•    When to use For and While statements.

•    The role of objects, properties, methods, and events.

•    How to set the tab order for controls.

•    The difference between macros, functions, and procedures.

•    How to call procedures containing arguments.

•    The difference between arguments and parameters.

•    What programs and applications are.

Next post:

Previous post: