有时使用DAO还是ADO是一个棘手的问题。 通过简单地更改编译时常量的值,
USEDAO ,您可以选择在代码中使用任一对象库。 这是遵循的过程: 1.设置对DAO和ADO对象库的引用。 在我的具体情况下,这将是:Microsoft DAO 3.6 Object Library
Microsoft ActiveX Data Objects 2.1 Object Library
2.在表单代码模块的“一般声明”部分中声明您的编译时常数:
#Const USEDAO = True 'will use DAO, for now
3.示例演示代码:
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
4.如果要在本练习中使用ADO,请将编译时常量更改为False:
#Const USEDAO = False 'will use ADO now
From: https://bytes.com/topic/access/insights/625936-dao-ado-why-not-both