AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1565118
Accepted
dutsnekcirf
dutsnekcirf
Asked: 2020-07-01 11:40:15 +0800 CST2020-07-01 11:40:15 +0800 CST 2020-07-01 11:40:15 +0800 CST

在 MS Access 中使用 VBA 将附件从一个记录集中的记录复制到另一个记录集中

  • 772

我几乎已经完成了这项工作。我在想我真的只需要另一双眼睛。我认为我的问题只是一个简单的编程结构问题。循环过多或以错误的顺序打开/关闭记录集。

我正在尝试将记录中包含的所有附件文件从一个 dao 记录集中复制到另一个 dao 记录集中的相应记录中。两个记录集都从同一个表中提取数据。第一个记录集 (rstOld) 包含具有去年日期值的记录,这些记录可以包含任意数量的附件。第二个记录集 (rstNew) 包含具有今年日期值的记录,并且这些记录不包含任何附件。

为此,我开始循环遍历 rstNew 中的每条记录。对于 rstNew 中的每条记录,我将收集 Name 字段的值,然后开始第二个循环。第二个循环将在 rstOld 中找到具有匹配名称字段的记录。从那里我只需将所有附件从 rstOld 中的记录复制到 rstNew 中的记录。

奇怪的是,它会在 rstNew 中找到匹配项的第一条记录上正常工作。之后,它不再适用于任何后续记录。

到目前为止,这是我的代码:

    Dim db As Database
    Dim strOldSQL As String
    Dim rstOld As DAO.Recordset2
    Dim strNewSQL As String
    Dim rstNew As DAO.Recordset2
    Dim rstOldAttachments As DAO.Recordset2
    Dim rstNewAttachments As DAO.Recordset2
    Dim strCurrentSiteName As String
    Dim strOldSiteName As String
Set db = CurrentDb()

    'First let's open a recordset that contains all of the records from this year.
    strNewSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, tblAuditForms.AuditYear FROM tblAuditForms WHERE AuditYear = #" & Format(cboMyDate, "mm/dd/yyyy") & "# ORDER By tblAuditForms.SiteName;"
    Set rstNew = db.OpenRecordset(strNewSQL)
    rstNew.MoveFirst
    rstNew.Edit
    
    Do While Not rstNew.EOF 'Now we need to loop through these records.
    
        strCurrentSiteName = rstNew.Fields("SiteName").Value 'Get the name of the site for the current record that we're on. We'll use this to compare with the sites in the previous audit.
                    
        'Now let's open a recordset that contains all records from the previous audit.
        strOldSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, Year([AuditYear]) FROM tblAuditForms WHERE Year([AuditYear]) = " & Me.cboPreviousDate & " ORDER BY tblAuditForms.SiteName;"
        Set rstOld = db.OpenRecordset(strOldSQL)
        rstOld.MoveFirst
        
        Do While Not rstOld.EOF 'Loop through each of the records from the previous audit until we find a record that matches the current site name.
        
            strOldSiteName = rstOld.Fields("SiteName").Value
        
            If strCurrentSiteName = strOldSiteName Then 'If this is true, then we've found a record from the previous audit that matches the one from our current audit.
                'Now it's just a matter of copying the attachments from the old record into the new one.  Working with attachments is annoying though.
                
                'This next block should loop through the attachments (if any) in the old record and copy them into the new record.
                Set rstOldAttachments = rstOld.Fields("Attachments").Value
                rstOldAttachments.MoveFirst
                
                Set rstNewAttachments = rstNew.Fields("Attachments").Value

                Do While Not rstOldAttachments.EOF
                    
                    rstNewAttachments.AddNew
                    rstNewAttachments.Fields("FileData").Value = rstOldAttachments.Fields("FileData").Value
                    rstNewAttachments.Fields("FileName").Value = rstOldAttachments.Fields("FileName").Value
                    rstNewAttachments.Fields("FileType").Value = rstOldAttachments.Fields("FileType").Value
                    rstNewAttachments.Update
                
                    rstOldAttachments.MoveNext
                Loop
                
                'Now that we've found the site from the previous audit and copied its attachments into the new record we can close the old recordset and move onto the next site in the current audit.
                rstOldAttachments.Close
                rstNewAttachments.Close
                Exit Do
            
            End If
                        
            rstOld.MoveNext
        Loop
         
        rstOld.Close
        rstNew.Update
        rstNew.MoveNext
    Loop
        
    'If we've gotten this far then we've looped through all of the new records that we just created from the weekly staffing workbook.
    rstNew.Close
    
    

就像我之前说的,这段代码将在通过 rstNew 记录集的第一个循环上工作,但在任何后续循环上都不起作用。我是否过早地摆脱了循环?或过早关闭记录集?

vba microsoft-access
  • 1 1 个回答
  • 776 Views

1 个回答

  • Voted
  1. Best Answer
    dutsnekcirf
    2020-07-01T13:23:50+08:002020-07-01T13:23:50+08:00

    我想通了!我知道我很接近。我了解到,一旦您执行了 recordset.update(或在我的情况下为 rstNew.update)语句,recordset editmode 属性就会回到 0。这可以解释为什么它会在第一个循环中成功复制附件但失败在任何后续循环上。所以我所要做的就是将“rstNew.Edit”语句直接移动到“Set rstNewAttachments = rstNew.Fields(“Attachments”).Value”行的上方。

    新代码如下所示:

        Dim db As Database
        Dim strOldSQL As String
        Dim rstOld As DAO.Recordset2
        Dim strNewSQL As String
        Dim rstNew As DAO.Recordset2
        Dim rstOldAttachments As DAO.Recordset2
        Dim rstNewAttachments As DAO.Recordset2
        Dim strCurrentSiteName As String
        Dim strOldSiteName As String
    Set db = CurrentDb()
    
        'First let's open a recordset that contains all of the records from this year.
        strNewSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, tblAuditForms.AuditYear FROM tblAuditForms WHERE AuditYear = #" & Format(cboMyDate, "mm/dd/yyyy") & "# ORDER By tblAuditForms.SiteName;"
        Set rstNew = db.OpenRecordset(strNewSQL)
        rstNew.MoveFirst
            
        Do While Not rstNew.EOF 'Now we need to loop through these records.
        
            strCurrentSiteName = rstNew.Fields("SiteName").Value 'Get the name of the site for the current record that we're on. We'll use this to compare with the sites in the previous audit.
                        
            'Now let's open a recordset that contains all records from the previous audit.
            strOldSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, Year([AuditYear]) FROM tblAuditForms WHERE Year([AuditYear]) = " & Me.cboPreviousDate & " ORDER BY tblAuditForms.SiteName;"
            Set rstOld = db.OpenRecordset(strOldSQL)
            rstOld.MoveFirst
            
            Do While Not rstOld.EOF 'Loop through each of the records from the previous audit until we find a record that matches the current site name.
            
                strOldSiteName = rstOld.Fields("SiteName").Value
            
                If strCurrentSiteName = strOldSiteName Then 'If this is true, then we've found a record from the previous audit that matches the one from our current audit.
                    'Now it's just a matter of copying the attachments from the old record into the new one.  Working with attachments is annoying though.
                    
                    'This next block should loop through the attachments (if any) in the old record and copy them into the new record.
                    Set rstOldAttachments = rstOld.Fields("Attachments").Value
                    rstOldAttachments.MoveFirst
                    
                    rstNew.Edit
                    Set rstNewAttachments = rstNew.Fields("Attachments").Value
    
                    Do While Not rstOldAttachments.EOF
                        
                        rstNewAttachments.AddNew
                        rstNewAttachments.Fields("FileData").Value = rstOldAttachments.Fields("FileData").Value
                        rstNewAttachments.Fields("FileName").Value = rstOldAttachments.Fields("FileName").Value
                        rstNewAttachments.Fields("FileType").Value = rstOldAttachments.Fields("FileType").Value
                        rstNewAttachments.Update
                    
                        rstOldAttachments.MoveNext
                    Loop
                    
                    'Now that we've found the site from the previous audit and copied its attachments into the new record we can close the old recordset and move onto the next site in the current audit.
                    rstOldAttachments.Close
                    rstNewAttachments.Close
                    Exit Do
                
                End If
                            
                rstOld.MoveNext
            Loop
             
            rstOld.Close
            rstNew.Update
            rstNew.MoveNext
        Loop
            
        'If we've gotten this far then we've looped through all of the new records that we just created from the weekly staffing workbook.
        rstNew.Close
    
    • 0

相关问题

  • 将电子表格导入 Access 时,如何防止 Excel 自动分配数据字段类型?

  • VBA将Excel中两行的多列放在自己的行上

  • 从不同的工作簿vba复制不同的工作表

  • 计算符合多个条件的行数

  • VBA根据文件名重命名工作表

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve