Use '!' to reference column in a recordset : Recordset Field « Access « VBA / Excel / Access / Word






Use '!' to reference column in a recordset

 

Option Explicit

Sub intro()
  Dim conn As New Connection
  Dim rec As New Recordset
  Dim ws As Worksheet
  Dim sql$, i&
  Set ws = ThisWorkbook.Worksheets("intro")
  conn.Open "Provider=microsoft.jet.oledb.4.0;" + _
    "Data Source=" + ThisWorkbook.Path + "\nwind.mdb;"
  sql = "SELECT LastName, FirstName " & _
        "FROM employees ORDER BY LastName, FirstName"
  rec.Open sql, conn
  While Not rec.EOF
    i = i + 1
    ws.[a1].Cells(i) = rec!LastName + ", " + rec!FirstName
    rec.MoveNext
  Wend
  rec.Close: conn.Close
End Sub

 








Related examples in the same category

1.Retrieving Field Values
2.Get rows from Recordset
3.Accessing Data with the Fields Collection and Field Objects