适用于:Access 2013、Office 2013
语法
表达式 。DefaultCursorDriver
表达式 一个表示 Workspace 对象的变量。
备注
可以将该设置值或返回值设置为 CursorDriverEnum 常量之一。
该属性设置仅影响在设置了该属性后建立的连接。 更改 DefaultCursorDriver 属性对现有的连接没有影响。
示例
以下示例使用 NextRecordset 方法查看 SELECT 复合查询中的数据。 在执行此类查询时, DefaultCursorDriver 属性必须设置为 dbUseODBCCursor。 即使部分或全部 SELECT 语句返回零记录, NextRecordset 方法也会返回 True;仅当检查了所有单个 SQL 子句后,它才返回 False。
Sub NextRecordsetX()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim rstTemp As Recordset
Dim intCount As Integer
Dim booNext As Boolean
' Create ODBCDirect Workspace object and open Connection
' object. The DefaultCursorDriver setting is required
' when using compound SQL statements.
Set wrkODBC = CreateWorkspace("", _
"admin", "", dbUseODBC)
wrkODBC.DefaultCursorDriver = dbUseODBCCursor
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Publishers", , , _
"ODBC;DATABASE=pubs;DSN=Publishers")
' Construct compound SELECT statement.
Set rstTemp = conPubs.OpenRecordset("SELECT * " & _
"FROM authors; " & _
"SELECT * FROM stores; " & _
"SELECT * FROM jobs")
' Try printing results from each of the three SELECT
' statements.
booNext = True
intCount = 1
With rstTemp
Do While booNext
Debug.Print "Contents of recordset #" & intCount
Do While Not .EOF
Debug.Print , .Fields(0), .Fields(1)
.MoveNext
Loop
booNext = .NextRecordset
Debug.Print " rstTemp.NextRecordset = " & _
booNext
intCount = intCount + 1
Loop
End With
rstTemp.Close
conPubs.Close
wrkODBC.Close
End Sub
完成同一任务的另一种方法是创建一个包含 SQL 复合语句的预备语句。 QueryDef 对象的 CacheSize 属性必须设置为 1, Recordset 对象必须为仅向前类型,并且是只读的。
Sub NextRecordsetX2()
Dim wrkODBC As Workspace
Dim conPubs As Connection
Dim qdfTemp As QueryDef
Dim rstTemp As Recordset
Dim intCount As Integer
Dim booNext As Boolean
' Create ODBCDirect Workspace object and open Connection
' object. The DefaultCursorDriver setting is required
' when using compound SQL statements.
Set wrkODBC = CreateWorkspace("", _
"admin", "", dbUseODBC)
wrkODBC.DefaultCursorDriver = dbUseODBCCursor
' Note: The DSN referenced below must be set to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conPubs = wrkODBC.OpenConnection("Publishers", , , _
"ODBC;DATABASE=pubs;DSN=Publishers")
' Create a temporary stored procedure with a compound
' SELECT statement.
Set qdfTemp = conPubs.CreateQueryDef("", _
"SELECT * FROM authors; " & _
"SELECT * FROM stores; " & _
"SELECT * FROM jobs")
' Set CacheSize and open Recordset object with arguments
' that will allow access to multiple recordsets.
qdfTemp.CacheSize = 1
Set rstTemp = qdfTemp.OpenRecordset(dbOpenForwardOnly, _
dbReadOnly)
' Try printing results from each of the three SELECT
' statements.
booNext = True
intCount = 1
With rstTemp
Do While booNext
Debug.Print "Contents of recordset #" & intCount
Do While Not .EOF
Debug.Print , .Fields(0), .Fields(1)
.MoveNext
Loop
booNext = .NextRecordset
Debug.Print " rstTemp.NextRecordset = " & _
booNext
intCount = intCount + 1
Loop
End With
rstTemp.Close
qdfTemp.Close
conPubs.Close
wrkODBC.Close
End Sub
以下示例使用 RecordStatus 和 DefaultCursorDriver 属性演示如何在批更新中跟踪对本地 Recordset 的更改。 若要使该过程运行,需要使用 RecordStatusOutput 函数。
Sub RecordStatusX()
Dim wrkMain As Workspace
Dim conMain As Connection
Dim rstTemp As Recordset
Set wrkMain = CreateWorkspace("ODBCWorkspace", _
"admin", "", dbUseODBC)
' This DefaultCursorDriver setting is required for
' batch updating.
wrkMain.DefaultCursorDriver = dbUseClientBatchCursor
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
Set conMain = wrkMain.OpenConnection("Publishers", _
dbDriverNoPrompt, False, _
"ODBC;DATABASE=pubs;DSN=Publishers")
' The following locking argument is required for
' batch updating.
Set rstTemp = conMain.OpenRecordset( _
"SELECT * FROM authors", dbOpenDynaset, 0, _
dbOptimisticBatch)
With rstTemp
.MoveFirst
Debug.Print "Original record: " & !au_lname
Debug.Print , RecordStatusOutput2(.RecordStatus)
.Edit
!au_lname = "Bowen"
.Update
Debug.Print "Edited record: " & !au_lname
Debug.Print , RecordStatusOutput2(.RecordStatus)
.AddNew
!au_lname = "NewName"
.Update
Debug.Print "New record: " & !au_lname
Debug.Print , RecordStatusOutput2(.RecordStatus)
.Delete
Debug.Print "Deleted record: " & !au_lname
Debug.Print , RecordStatusOutput2(.RecordStatus)
' Close the local recordset without updating the
' data on the server.
.Close
End With
conMain.Close
wrkMain.Close
End Sub
Function RecordStatusOutput(lngTemp As Long) As String
Dim strTemp As String
strTemp = ""
' Construct an output string based on the RecordStatus
' value.
If lngTemp = dbRecordUnmodified Then _
strTemp = "[dbRecordUnmodified]"
If lngTemp = dbRecordModified Then _
strTemp = "[dbRecordModified]"
If lngTemp = dbRecordNew Then _
strTemp = "[dbRecordNew]"
If lngTemp = dbRecordDeleted Then _
strTemp = "[dbRecordDeleted]"
If lngTemp = dbRecordDBDeleted Then _
strTemp = "[dbRecordDBDeleted]"
RecordStatusOutput = strTemp
End Function