DAO中Recordset2对象与Recordset的区别-宏鹏
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


DAO中Recordset2对象与Recordset的区别

发表时间:2014/8/7 8:17:49 评论(0) 浏览(11030)  评论 | 加入收藏 | 复制
   
摘 要:Recordset2 对象包含所有与 Recordset 对象相同的属性和方法,并且 Recordset2 对象还包含一个支持多值字段类型的新属性:ParentRecordset。
正 文:

Recordset2 Object (DAO)

Office 2013
上次修改时间: 2011年7月1日

适用范围: Access 2013 | Office 2013

本文内容 
注解 
示例 
有关参与者 

Recordset2对象代表在基本表中的记录或从运行查询产生的记录。

Recordset2 对象包含所有与 Recordset 对象相同的属性和方法,并且 Recordset2 对象还包含一个支持多值字段类型的新属性:ParentRecordset

下面的示例演示如何导航Recordset包含多值字段。

示例由提供的代码: Microsoft Access 2010 程序员参考 |有关参与者

VBA

Sub PrintStudentsAndClasses()
    Dim dbs As DAO.Database
    Dim rsStudents As DAO.Recordset2  'Recordset for students
    Dim rsClasses As DAO.Recordset2  'Recordset for classes
    Dim fld As DAO.Field2

    'open the database
    Set dbs = CurrentDb()

    'get the table of students
    Set rsStudents = dbs.OpenRecordset("tblStudents")

    'loop through the students
    Do While Not rsStudents.EOF
        
        'get the classes field
        Set fld = rsStudents("Classes")

        'get the classes Recordset
        'make sure the field is a multi-valued field before
        'getting a Recordset object
        If fld.IsComplex Then
            Set rsClasses = fld.Value
        End If

        'access all records in the Recordset
        If Not (rsClasses.BOF And rsClasses.EOF) Then
            rsClasses.MoveLast
            rsClasses.MoveFirst
        End If

        'print the student and number of classes
        Debug.Print rsStudents("FirstName") & " " & rsStudents("LastName"), _
            "Number of classes: " & rsClasses.RecordCount

        'print the classes for this student
        Do While Not rsClasses.EOF
            Debug.Print , rsClasses("Value")
            rsClasses.MoveNext
        Loop

        'close the Classes Recordset
        rsClasses.Close

        'get the next student
        rsStudents.MoveNext

    Loop
    
    'cleanup
    rsStudents.Close

    Set fld = Nothing
    Set rsStudents = Nothing
    Set dbs = Nothing

End Sub

下面的示例演示如何导航中的附件字段的文件。在立即窗口中打印的文件类型和每个附件的文件名。

VBA

Sub ListAttachments()
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset2
    Dim rsA As DAO.Recordset2
    Dim fld As DAO.Field2
    
    'Get the database, recordset, and attachment field
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblAttachments")
    Set fld = rst("Attachments")
    
    'Navigate through the table
    Do While Not rst.EOF
    
        'Print the first and last name
        Debug.Print rst("FirstName") & " " & rst("LastName")
        
        'Get the recordset for the Attachments field
        Set rsA = fld.Value
        
        'Print all attachments in the field
        Do While Not rsA.EOF
            Debug.Print , rsA("FileType"), rsA("FileName")
            
            'Next attachment
            rsA.MoveNext
        Loop
        
        rsA.Close
        
        'Next record
        rst.MoveNext
    Loop
    
        
    rst.Close
    dbs.Close
    
    Set fld = Nothing
    Set rsA = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Sub

下面的示例演示如何将文件从指定的文件夹路径添加到附件字段。

VBA

Public Function LoadAttachments(strPath As String, Optional strPattern As String = "*.*") As Long
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset2
    Dim rsA As DAO.Recordset2
    Dim fld  As DAO.Field2
    Dim strFile As String
    
    'Get the database, recordset, and attachment field
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblAttachments")
    Set fld = rst("Attachments")
    
    'Navigate through the table
    Do While Not rst.EOF
    
        'Get the recordset for the Attachments field
        Set rsA = fld.Value
        
        'Load all attachments in the specified directory
        strFile = Dir(strPath & "\*.*")
        
        rst.Edit
        Do While Len(strFile) > 0
            'Add a new attachment that matches the pattern.
            'Pass "" to match all files.If strFile Like strPattern Then
                rsA.AddNew
                rsA("FileData").LoadFromFile strPath & "\" & strFile
                rsA.Update
                
                'Increment the number of files added
                LoadAttachments = LoadAttachments + 1
            End If
            strFile = Dir
        Loop
        rsA.Close
        
        rst.Update
        'Next record
        rst.MoveNext
    Loop
    
    rst.Close
    dbs.Close
    
    Set fld = Nothing
    Set rsA = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Function

下面的示例演示如何将保存到指定的文件夹路径附件字段中存储的文件。

VBA

Public Function SaveAttachments(strPath As String, Optional strPattern As String = "*.*") As Long
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset2
    Dim rsA As DAO.Recordset2
    Dim fld As DAO.Field2
    Dim strFullPath As String
    
    'Get the database, recordset, and attachment field
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblAttachments")
    Set fld = rst("Attachments")
    
    'Navigate through the table
    Do While Not rst.EOF
    
        'Get the recordset for the Attachments field
        Set rsA = fld.Value
        
        'Save all attachments in the field
        Do While Not rsA.EOF
            If rsA("FileName") Like strPattern Then
                strFullPath = strPath & "\" & rsA("FileName")
                
                'Make sure the file does not exist and save
                If Dir(strFullPath) = "" Then
                    rsA("FileData").SaveToFile strFullPath
                End If
                
                'Increment the number of files saved
                SaveAttachments = SaveAttachments + 1
            End If
            
            'Next attachment
            rsA.MoveNext
        Loop
        rsA.Close
        
        'Next record
        rst.MoveNext
    Loop
    
    rst.Close
    dbs.Close
    
    Set fld = Nothing
    Set rsA = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Function

下面的示例演示如何删除在附件字段中存储的文件。

VBA

Function RemoveAttachment(strRemoveFile As String, Optional strFilter As String) As Long
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset2
    Dim rsA As DAO.Recordset2
    Dim fld As DAO.Field2
    
    'Get the database
    Set dbs = CurrentDb
    
    'Open the recordset.If the strFilter is supplied, add it to the Where
    'clause for the recordset.Otherwise, any files matching strFileName
    'will be deleted
    If Len(strFilter) > 0 Then
        Set rst = dbs.OpenRecordset("Select * FROM tblAttachments Where " & strFilter)
    Else
        Set rst = dbs.OpenRecordset("tblAttachments")
    End If
    
    'Get the Attachment field
    Set fld = rst("Attachments")
    
    'Navigate through the recordset
    Do While Not rst.EOF
    
        'Get the recordset for the Attachments field
        Set rsA = fld.Value
        
        'Walk the attachments and look for the file name to remove
        Do While Not rsA.EOF
            If rsA("FileName") Like strRemoveFile Then
                rsA.Delete
                
                'Increment the number of files removed
                RemoveAttachment = RemoveAttachment + 1
            End If
            rsA.MoveNext
        Loop
                
        'Cleanup the Attachments recordset
        rsA.Close
        Set rsA = Nothing
        
        'Next record
        rst.MoveNext
    Loop
    
    rst.Close
    dbs.Close
    Set fld = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Function


Access软件网交流QQ群(群号:198465573)
 
 相关文章
Recordset记录集对象的属性和方法(一)  【宏鹏(转载)  2012/7/18】
Recordset记录集对象的属性和方法(二)  【宏鹏(转载)  2012/7/18】
一句话让程序运行速度提高了几百倍(recordset的Cursor...  【风行(转载)  2012/9/1】
Access开发人员参考:RecordsetTypeEnum枚举与...  【红尘如烟  2012/12/25】
Recordset对象  【小英  2013/1/29】
常见问答
技术分类
相关资源
文章搜索
关于作者

宏鹏

文章分类

文章存档

友情链接