Objects in Access zGeneral Access objects zData access objects (DAO) yused for data management xdata definition xdata manipulation yused only in code modules
Commonly Used Data Access Objects
Using DAO zUsing the current database declare an object variable of type Database use the CurrentDb function to store a reference to the current database yexample Dim dbs As Database Set dbs = CurrentDb( )
Using DAO zLooping through collections uses For Each statement, similar to a For Next loop yexample Dim dbs As Database Dim conTest As Container Set dbs = CurrentDb() For Each conTest In dbs.Containers Debug.Print “Container: “; conTest.Name Next
Using DAO Recordsets yrecords belonging to a table or query ymost commonly used types xtable-type xdynaset-type xsnapshot-type
Using DAO Table-type recordsets default type for any recordset object ycan use indexes to speed up the process of searching for records yexample Dim rec As Recordset Set rec = db.OpenRecordset(“Students”)
Using DAO Dynaset-type recordsets ycan contain a table or the result of a query ycan be edited and the results will be reflected in the underlying tables useful if the recordset object is very large yexample Set rec = db.OpenRecordset(“Students”, dbOpenDynaset)
Using DAO Snapshot-type ynot updateable yexample Set rec = db.OpenRecordset(“Students”, dbOpenSnapshot)
Using DAO Moving through recordsets
Using DAO RecordsetClone method yalternative to OpenRecordset method ycreates copy of recordset that can be manipulated independently of the form’s recordset Bookmark property ywhat Access uses instead of record numbers ystored as an array of bytes yused to synchronize recordset objects which are clones of each other
Using DAO Finding records in table-type recordsets Seek method used with indexed fields xselect indexed field to search on xspecify search criteria xexample rec.Index = “Price” rec.Seek “=“, curPrice
Using DAO zFinding records in dynasets and snapshots Find method Works on non-indexed fields for all types of recordsets yFour methods xFindFirst xFindLast xFindNext xFindPrevious
Using DAO Methods to edit recordsets
Using DAO Editing field values in a recordset yexample rec.Edit rec!strField = NewValue rec.Update
Using DAO With … End With ylogic structure often used with DAOs, but can be used with controls and collections