Microsoft Access 2010 Bible

(Rick Simeone) #1

Part II: Programming Microsoft Access


506


Public Sub OpenDAORecordset1()
Dim db As Database
Dim rs As Recordset
Dim i As Long
Set db = CurrentDb
‘Open recordset directly against a table:
Set rs = db.OpenRecordset(“tblCustomers”)
Do While Not rs.EOF
For i = 0 To rs.Fields.Count - 1
Debug.Print rs.Fields(i).Name _
& “ “ & rs.Fields(i).Value
Next i
Debug.Print
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
db.Close
End Sub

The DAO Field objects (recordsets)
Field objects within recordsets represent a column of data from a table or returned by a query.
Recordset Field objects differ from their TableDef and QueryDef counterparts in that they
actually contain a data value. Each TableDef object contains a Fields collection containing the
data held within the table represented by the TableDef.

You’ll see many, many references to DAO (and ADO) fields in this book, so there isn’t much to dis-
cuss at this point. In the meantime, it’s enough to know that the DAO Field object supports
many more properties than are visible in the Access Table Designer. The Chapter13.accdb
example database includes the following procedure that enumerates all the “valid” properties of the
Company field in tblCustomers:

Public Sub DisplayFieldProperties()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim prop As DAO.Property
Set db = CurrentDb
Set tdf = db.TableDefs(“tblCustomers”)
Set fld = tdf.Fields(“Company”)
Debug.Print “Properties in Company field:”
On Error Resume Next
For Each prop In fld.Properties
On Error Resume Next
Debug.Print “ “ & prop.Name & “ = “ & prop.Value
On Error GoTo 0
Next prop
db.Close
End Sub
Free download pdf