Accessing Data with the Fields Collection and Field Objects : Recordset Field « Access « VBA / Excel / Access / Word






Accessing Data with the Fields Collection and Field Objects

 
Sub field()
    Dim dbNorthwind As DAO.Database
    Dim dbPath As String
    DbPath = CurrentProject.Path & "\mydb.mdb"
    Set dbNorthwind = OpenDatabase(dbPath)
    
    Dim rsEmployees As DAO.Recordset
    Dim rsCustomers As DAO.Recordset
    Set rsEmployees = dbNorthwind.OpenRecordset("Employees", dbOpenTable)
    Set rsCustomers = dbNorthwind.OpenRecordset("Customers", dbOpenTable)

    I = 2
    With rsEmployees
        If Not .BOF Then .MoveFirst
        Do While Not .EOF
            Cells(I, 1).Value = .Fields(2)  'First name in third column
            Cells(I, 2).Value = .Fields(1)  'Last name in second column
            .MoveNext
            I = I + 1
        Loop
    End With

End Sub

 








Related examples in the same category

1.Retrieving Field Values
2.Get rows from Recordset
3.Use '!' to reference column in a recordset