1. Set References to both the DAO and ADO Object Libraries. In my specific case, this would be:
Expand|Select|Wrap|Line Numbers
- Microsoft DAO 3.6 Object Library
- Microsoft ActiveX Data Objects 2.1 Object Library
Expand|Select|Wrap|Line Numbers
- #Const USEDAO = True 'will use DAO, for now
Expand|Select|Wrap|Line Numbers
- Dim strSQL As String
- strSQL = "SELECT * FROM Employees ORDER BY [LastName]"
- #If USEDAO Then '#Const USEDAO = True
- Dim MyDB As DAO.Database, MyRS As DAO.Recordset
- Set MyDB = CurrentDb()
- Set MyRS = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
- Do While Not MyRS.EOF
- 'Example of Recordset processing
- Debug.Print MyRS![FirstName] & " " & MyRS![LastName]
- MyRS.MoveNext
- Loop
- #Else '#Const USEDAO = False, USE ADO
- Dim MyRS As ADODB.Recordset
- Set MyRS = New ADODB.Recordset
- Set MyRS.ActiveConnection = CurrentProject.Connection
- With MyRS
- .CursorType = adOpenStatic
- .LockType = adLockReadOnly
- .Open strSQL
- End With
- 'You can also use the single line syntax
- 'MyRS.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly, adCmdText
- Do While Not MyRS.EOF
- 'Example of Recordset processing
- Debug.Print MyRS![FirstName] & " " & MyRS![LastName]
- MyRS.MoveNext
- Loop
- #End If
Expand|Select|Wrap|Line Numbers
- #Const USEDAO = False 'will use ADO now