|
The real power of
a database is derived from the way you can organize, display and summarize information.
You have learned how to put data into databases using VBA and forms. Now it is time to get
some meaningful information out. The process of
normalization eliminates redundant information and inconsistences within our database, but
it also splits data up into separate tables that we might want to see as a whole. If we do
want to view information as a whole, we need to be able to manipulate both records and
sets of records with VBA, in the same way that we=ve learned to manipulate forms. What is a Recordset?
Even if you do not
know what a Recordset is, you have already used
one. Put simply, a Recordset is just what it
says - a set of records. When you open a table in Datasheet view, you are looking at a set
of records. When you open a form, it will normally have a set of records behind it which
supply the data for the form. Databases are sets of records, and you will find that you
make extensive use of Recordset objects
throughout your VBA code. Example:
Private Sub OpeningARecordset() Run the procedure by pressing <F5> or clicking the Run button. You should get the following message box appear.
Explaining the Code1. The dim statements include new types. Database and Recordset types are the new types, both of these are objects and therefore we can determine which object to use. 2. The line Set db = CurrentDb() puts the name of the open database into the variable db. 3. The line Set rec = db.OpenRecordset(Whisky) places the contents of the Whisky table into the recordset object variable rec by using the OpenRecordset method. 4. The line intRecords = rec.RecordCount uses the RecordCount method to count the number of records in the recordset defined by rec. 5. The message box line you should understand. 6. The line rec.Close uses the Close method to remove the recordset rec from memory. |