Share this Page URL

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

Using Visual Basic to Automate Your Database 163 This procedure uses Do While...Loop and the AddItem method of the list box control to display each country from the MarketingCampaigns table in the list box. It also uses the DAO object model to gain access to the data. We'll cover details about DAO later in this chapter; in short, the procedure assigns a reference to the MarketingCampaigns table to the variable named rs. As long as we haven't reached the end of the recordset--as long as the EOF (for end of file) property of the recordset is False--the procedure adds each country to the list box (using the AddItem method) and then moves to the next record. Do loops vary depending on when you want instructions to execute. For example, you can use Until rather than While to run statements until a certain condition is true. You can also place While or Until before the Loop statement or after it. The Demonstrations module contains another example of a Do...While statement you can run in the Immediate window. The DoWhile procedure displays the values from the Country and Launch- Date fields from the MarketingCampaigns table. The While...Wend Statement A While...Wend statement is another looping structure that executes a series of statements while a given condition is true. You'll probably use the Do...Loop statement more often than While... Wend, but here's an example. As in the previous example, this procedure uses methods of the DAO Recordset object to move from record to record in a recordset. As long as the end of the recordset ( EOF ) hasn't been reached, the procedure displays the last name of the employees in the Employ- ees table in the Immediate window. This procedure is included in modDemonstrations. Public Sub WhileWend() Dim db as Database Dim rs as Recordset