“保持”记录集

大多数访问用户意识到,记录集是查询,表或SQL语句的虚拟表示形式,仅存在于我们PC的内存中。 它们及其包含的数据实际上在某个特定时间存在-然后在下一个特定时间出现。 但是,我们很少有人意识到,可以将它们保存到磁盘上,然后再将其恢复为遗嘱。 技术术语称为“持久记录集”,我将向您展示如何完成。 ADO具有将记录集持久保存到磁盘上文件的独特功能。 您还可以稍后重新打开它,对其进行编辑,将其重新连接到原始数据源并保存更改。 要将Recordset保留到磁盘上以供以后使用,请调用其Save方法。

rst.Save Filename, Format
Filename参数是您要用来保存Recordset内容的文件的完整路径和文件名
格式参数可以是2个固有常数之一:
  1. adPersistADTG(默认)-以Microsoft专有的高级数据表格式保存记录集。
  2. adPersistXML-将记录集另存为XML。 如果以XML格式保存Recordset,则可以轻松地将保存的XML文件用作另一个了解XML的应用程序或控件的数据源。 XML是用于传输数据的新兴Internet标准。
注意: ADTG文件小于XML文件,因此,除非您需要以XML格式分发数据的能力,否则请坚持使用ADTG。

在“概述”中有足够的内容-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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值