我正在寻找一种创建内存数据表表单的方法。我喜欢使用断开连接的记录集的想法。但在将断开连接的记录集重新分配给表单后,我遇到了应用程序崩溃的问题。在 stackoverflow 上,HK1 先生的帖子提示了我,他在其中写了以下内容:
如果从表中获取记录集(即使是空表) 然后断开连接就可以解决这个问题。
但是要怎么做呢?对于 DAO.Recordset,我收到错误:此类型的对象不支持操作。我尝试了所有类型的记录集,但没有帮助:
Sub testDynaset()
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("MyLocalTable", dbOpenDynaset)
rs.Connection.Close ' << error: Operation is not supported for this type of object
End Sub
Sub testForwardOnly()
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("MyLocalTable", dbOpenForwardOnly)
rs.Connection.Close ' << error: Operation is not supported for this type of object
End Sub
Sub testOpenSnapshot()
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("MyLocalTable", dbOpenSnapshot)
rs.Connection.Close ' << error: Operation is not supported for this type of object
End Sub
Sub testOpenTable()
Dim rs As dao.Recordset
Set rs = CurrentDb.OpenRecordset("MyLocalTable", dbOpenTable)
rs.Connection.Close ' << error: Operation is not supported for this type of object
End Sub
对于 ADODB.Recordset,我不知道如何从本机本地或链接的 MS ACCESS 表获取它。请帮忙!
您可以使用
CurrentProject.Connection
,它是与当前数据库的 ADO 连接。它支持本地表和链接表。
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM MyLocalTable", CurrentProject.Connection, adOpenStatic, adLockReadOnly
Set rs.ActiveConnection = Nothing
根据您具体想要执行的操作,您可能需要调整记录集和锁定类型。我不完全确定为什么要使用内存中记录集,普通的静态记录集已经包含内存中记录的副本,并且不会重新获取它们。