Share this Page URL

Chapter 5. Using Visual Basic to Automat... > Creating an Event Procedure - Pg. 143

Using Visual Basic to Automate Your Database 143 3. object variables. We'll use these variables to refer to a table in the current database. The third statement defines a variable named int Answer and declares it to be an integer. We'll use this variable to store the value associated with the button a user clicks in a message box. We'll create the message box by using the MsgBox function later in this procedure. Following the three Dim statements, enter the lines of code shown here: Set db = CurrentDb Set tbl = db.TableDefs("CampaignExpenses") 4. These statements assign references to the object variables we declared. The variable db (which is declared with the type Database ) is assigned to the database that's currently open --the HelloWorld database. The variable tbl is assigned a reference to the CampaignExpen- ses table. TableDefs is an object that represents the collection of tables in the database. You'll learn more about collections in the section "Using an Object Model," later in this chapter. Enter this statement next: intAnswer = MsgBox("Campaign Expenses were last updated on " & _ tbl.LastUpdated, vbYesNo + vbQuestion, _ "Do You Want To Update the Expense Table?") This statement has quite a bit going on. On a purely practical side, the statement shows how you can use the Visual Basic line continuation character (the underscore, _) to break up a single statement so that it's more readable. Without the line continuation characters, this code needs to appear on a single line for Visual Basic to understand it. You and others reading the code need to scroll far to the right to see the entire statement. On the functional side, this statement assigns to the variable named int Answer the value that's returned by the MsgBox function. Remember that the difference between a function and a Sub procedure is that a function returns a value. MsgBox is a built-in function that displays a