【问题标题】:Looping through attachment filename MS Access遍历附件文件名 MS Access
【发布时间】:2022-12-07 07:20:49
【问题描述】:

我有一种情况需要遍历一个控件,该控件将附件的文件名加载到 MS Access 中的记录中。在主窗体上,我有一个名为“附件”的附件控件(您可以在其中管理/添加/删除附件),一个名为“RecordOfChanges”的绑定长文本控件,以及一个名为 SF_AttachmentsList 的子窗体,其中包含显示列表的控件的文件名。

我想要做的是捕获变量中的文件名,并在我想放置的每个文件名的末尾添加“:[注意此处对此附件的更改。如果没有,请放置“无更改”。或删除此行如果不适用。]”。最后,我想将该变量放入“更改记录”文本字段中。 因此,例如,如果文件名为 Doc1.docx、Doc2.xlsx 和 Doc3.Pdf,则更改记录字段应如下所示。

doc1.docx:[请注意此处附件的更改。如果没有,请输入“无变化”。如果不适用,或删除此行。]

Doc2.xlsx:[请注意此处对此附件的更改。如果没有,请输入“无变化”。如果不适用,或删除此行。]

Doc3.Pdf[注意这里附件的变化。如果没有,请输入“无变化”。如果不适用,或删除此行。]

这是我到目前为止尝试过的代码,我收到错误“运行时错误 451。未定义属性让过程和属性获取过程未返回对象 VBA”。调试器突出显示行 strFileName = Forms!Attachments!SF_AttachmentList!......

任何帮助将不胜感激。我希望我已经为任何愿意提供帮助的人提供了足够的细节。如果没有,请告诉我缺少哪些细节,我会提供这些信息。

Private Sub BtnEditSOW_Click()
Dim i As Long
Dim varChanges As String
Dim Count As Integer
Dim strFileName As String
'Counting the number of attachements from attachment control to set the number of loops needed
Count = Me.Attachments.AttachmentCount
'loop thourgh all items in attachments.filename control
For i = 0 To Count
    'capture the text string of each file name and concatinate instructions to the end of each file name. <<<This line is where I get the error>>>
    strFileName = Forms!Attachments!SF_AttachmentsList!FileName(i) & ": [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]"
    'add file name text to variable varChanges
    varChanges = varChanges & Chr(13) & Chr(10) & strFileName
Next i
'Put varChanges in the Record Of Changes text box control.
Me.Attachments_RecordOfChanges = varChanges
End Sub

【问题讨论】:

    标签: vba for-loop ms-access attachment


    【解决方案1】:

    我认为循环附件控件的快捷方式不会起作用(我试过)。打开记录集对象并循环记录。

    Private Sub BtnEditSOW_Click()
    Dim varChanges As String
    Dim rs As DAO.Recordset
    Dim strFileName As String
    Set rs = CurrentDb.OpenRecordset("SELECT Attachments.FileName FROM table WHERE ID=" & Me.ID)
    
    'loop through all items in recordset
    Do While Not rs.EOF
        'capture the text string of each file name and concatenate instructions to the end of each file name. <<<This line is where I get the error>>>
        strFileName = rs(0) & ": [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]"
        'add file name text to variable varChanges
        varChanges = varChanges & strFileName & Chr(13) & Chr(10)
        rs.MoveNext
    Loop
    'Put varChanges in the Record Of Changes text box control.
    Me.Attachments_RecordOfChanges = varChanges
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 2011-02-14
      相关资源
      最近更新 更多