【发布时间】:2015-07-10 23:23:20
【问题描述】:
使用 MS Access 2013:
我正在创建一个分离的 ADO 记录集,作为我正在编写的应用程序的内存写入缓冲区。通常我会使用表/查询,但多人同时使用数据库。如果我将这些记录写到表中,用户就有可能会覆盖彼此的数据。
ADODB.recordset 被定义为一个全局变量,并使用一个函数进行初始化。现在,这是棘手的部分。我可以很好地定义记录集,但是当我添加第一条记录时,添加后记录“消失”。
我使用 While-Wend 循环来添加记录。当我进入该循环的中间时,我可以移动到不同的记录,并 debug.print 任何我喜欢的值。但是当我退出循环的那一刻,我失去了所有的记录。无论我移动到哪里,任何 debug.print 命令都会产生“无当前记录”。当我在其他函数中调用全局变量时,我得到一个空记录集。
代码如下:
Option Compare Database
'Establish a global recordset variable to be a write buffer
Public gbl_rstADO_WriteBuffer As ADODB.Recordset
Option Explicit
Private Function InitWriteBuffer()
Dim strSQL As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim i As Integer
Dim j As Integer
'These records will form the structure and initial entries of the write buffer
strSQL = "SELECT [#DEFAULT MASTER QUERY].* FROM [#DEFAULT MASTER QUERY] INNER JOIN " & _
"tblBatchCircuitUploadTable ON [#DEFAULT MASTER QUERY].[Install ID] = " & _
"tblBatchCircuitUploadTable.[Install ID] WHERE (((tblBatchCircuitUploadTable.[Install ID]) Is Not Null));"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL)
Set gbl_rstADO_WriteBuffer = New ADODB.Recordset
'Create detatched recordset using fields from SQL query
For i = 0 To rst.Fields.Count - 1
gbl_rstADO_WriteBuffer.Fields.Append rst.Fields(i).Name, adVariant, , adFldMayBeNull
Next
With gbl_rstADO_WriteBuffer
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.LockType = adLockPessimistic
.Open
End With
rst.MoveFirst
'Copy values from DAO recordset, one-by-one, into the detatched ADO recordset
While Not rst.EOF
gbl_rstADO_WriteBuffer.AddNew
'Move through values of current source record, and copy one by one to ADO destination
For j = 0 To gbl_rstADO_WriteBuffer.Fields.Count - 1
gbl_rstADO_WriteBuffer.Fields(j).Value = rst.Fields(j)
Next
gbl_rstADO_WriteBuffer.Update
rst.MoveNext
Wend
'With the following command I receive a "No Current Record" error
'Moving to first, last, etc in the immediate window all still produce "no current record"
Debug.Print gbl_rstADO_WriteBuffer.Fields(0).Value
'Cleanup
Set rst = Nothing
Set dbs = Nothing
End Function
有人看到我遗漏的东西吗?
【问题讨论】:
-
根据Append Method (ADO),"ADO 不支持以下数据类型,并且不应在将新字段附加到记录集对象 (ADO) 时使用:adIDispatch、adIUnknown、adVariant。”
-
我认为这是一个过程,而不是一个编程问题。为什么不使用一个临时表来快照您的数据集而无需用户交互?为什么不让应用程序在运行时调用 ADO 或 ODBC 记录集,而不是在后端数据库中保存内存?最后,考虑保存recordset externally。
-
"通常我会使用一个表/查询,但是多个人同时使用数据库。如果我将这些记录写到一个表中,用户可能会覆盖彼此的数据。” - 如果您正确设置了多用户访问应用程序,则不会。每个用户必须拥有自己的前端(用户界面)本地副本,因此如果应用程序写入前端 .accdb/.accde 文件中的本地表,那么用户将 永远不能覆盖彼此的数据。
标签: ms-access vba ado ms-access-2013