Share this Page URL

Chapter 5. Using Visual Basic to Automat... > Using Control Structures - Pg. 157

Using Visual Basic to Automate Your Database 157 Using descriptive names for variables adds readability to your code. Even more than with database objects, you want to easily remember what a variable in your code refers to. Following a convention for naming variables is helpful, especially when more than one person is writing or reviewing code that's part of a database. Although Visual Basic is not case sensitive, capitalizing the start of indi- vidual words within a variable name is good practice--for example, strCustName for a variable that's used for a customer's name. Adding a prefix to a variable's name is also common practice. The prefix tells you immediately what type of variable you're dealing with, as in strMyString or intAnswer. Some programmers also use the letters g, m, and l as part of the prefix to indicate the scope of a variable. Table 5-3 lists some of the prefixes that you can use when naming variables. Table 5-3. Variable Name Prefixes Data Type Boolean Currency Date Double Integer Long Single String Variant Prefix bln cur dte dbl int lng sng str var Example blnApprovalRequired curAdBudget dteLaunchDate dblBigNumber intAnswer lngSalesTarget sngInterestRate strFirstName varAnswer Static Variables A static variable has the scope of a local variable but the lifetime of a module-level variable. A static variable holds its value even after the procedure in which the variable is declared ends. You won't use a static variable that often, but you might in a situation in which you are calculating a running total, for example. In a case such as this, you want the variable to retain its value. To declare a static variable, you use the Static statement, as shown here: Static blnName as Boolean In a form module, a static variable keeps its value until the form is closed. Using Control Structures To direct the operations you want your code to perform, you use the programming structures avail- able in Visual Basic. With these structures, you can define conditions under which specific instruc- tions are executed, you can repeat a set of instructions as long as a condition is true, and so on. As you saw earlier, the Import procedure includes an If...Then...Else statement, which we'll review in this section along with several other statements. To see some of these examples in action, we'll use the Debug.Print method and the Immediate window, the area of the Visual Basic Editor we introduced earlier that lets you observe and test the execution of your code. (The Immediate window is often used when debugging code. You'll learn more about debugging in the section "Handling Errors and Debugging Code," later in this chapter.) We'll set up the Immediate window in the section "The If...Then...Else Statement." If you want to run any of the other code samples, follow the steps provided for displaying the Immediate window, enter the procedure's name (and any required arguments) in the Immediate window, and then press Enter.