大多数访问用户意识到,记录集是查询,表或SQL语句的虚拟表示形式,仅存在于我们PC的内存中。 它们及其包含的数据实际上在某个特定时间存在-然后在下一个特定时间出现。 但是,我们很少有人意识到,可以将它们保存到磁盘上,然后再将其恢复为遗嘱。 技术术语称为“持久记录集”,我将向您展示如何完成。 ADO具有将记录集持久保存到磁盘上文件的独特功能。 您还可以稍后重新打开它,对其进行编辑,将其重新连接到原始数据源并保存更改。 要将Recordset保留到磁盘上以供以后使用,请调用其Save方法。
rst.Save Filename, Format
Filename参数是您要用来保存Recordset内容的文件的完整路径和文件名
格式参数可以是2个固有常数之一:
- adPersistADTG(默认)-以Microsoft专有的高级数据表格式保存记录集。
- adPersistXML-将记录集另存为XML。 如果以XML格式保存Recordset,则可以轻松地将保存的XML文件用作另一个了解XML的应用程序或控件的数据源。 XML是用于传输数据的新兴Internet标准。
在“概述”中有足够的内容-2个经过良好注释的子例程过程将演示如何将记录集保存(持久化)到磁盘上的文件,然后检索它,进行更改并将其保存回磁盘:
Public Sub SaveRecordset()
Dim rst As ADODB.Recordset
Dim strFile As String
Set rst = New ADODB.Recordset
'Open the recordset from the database
rst.Open "tblCustomers", CurrentProject.Connection, _
adOpenStatic, adLockOptimistic
'Construct a file name to use (ADTG or XML)
strFile = CurrentProject.Path & "\Customers.adtg" 'OR
'strFile = CurrentProject.Path & "\Customers.xml"
'Destroy any existing file. Necessary because the Save Method
will fail if the specified file already exists.
On Error Resume Next
Kill strFile
Err.Clear
'Now save the recordset to disk (ADTG Format)
rst.Save strFile, adPersistADTG
'Close the recordset in memory
rst.Close
End Sub
Public Sub RetrieveRecordset()
Dim rst As ADODB.Recordset
Dim strFile As String
Set rst = New ADODB.Recordset
'Construct a file name to use
strFile = CurrentProject.Path & "\Customers.adtg" 'OR
'strFile = CurrentProject.Path & "\Customers.xml"
'Make sure the file exists
If Len(Dir(strFile)) > 0 Then
'Open the recordset from the file
rst.Open strFile, , adOpenStatic, adLockOptimistic
'Reconnect the recordset to the database
rst.ActiveConnection = CurrentProject.Connection
'Make a change and save it
rst.Fields("ContactTitle") = "Sales Rep"
rst.Update
End If
rst.Close
Set rst = Nothing
End Sub
From: https://bytes.com/topic/access/insights/653114-persisting-recordset