Share this Page URL

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

Using Visual Basic to Automate Your Database 158 We'll also use a form to demonstrate some of the control structures. The form will give you a sense of how to use code to perform tasks in your database. Most of the code examples we present can be modified for use in databases that you create. You'll also see examples of these control structures in later chapters as we add more code to the HelloWorld database. Testing statements one at a time You can also use the Immediate window to test individual statements. To do so, enter one line at a time in the Immediate window, and press Enter to execute each line. The If...Then...Else Statement Here's a block of code from a version of the Import procedure that includes an If...Then...Else statement: If intAnswer = vbYes Then DoCmd.SetWarnings False DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _ "CampaignExpenses", _ "C:\microsoft press\accessinsidetrack\marketingexpenses.xls", _ True DoCmd.SetWarnings True Else MsgBox "Expenses Won't Be Updated" End If In an If...Then...Else statement, you define conditions that determine which set of actions a pro- cedure runs. If something happens, then do this; otherwise ( else ), do this. In Import, if a user clicks the Yes button (if the MsgBox function returns vbYes ), the DoCmd methods run. If a user clicks the