1

I have a situation where I need to loop through a control that has the file names of attachments loaded to a record in MS Access. On the main form, I have the attachment control itself named "Attachments"(where you can manage/add/remove attachments), a bound long text control called "RecordOfChanges", and a subform named SF_AttachmentsList which contains the control that shows the list of file names.

What I am trying to do is to capture the file names in a variable, and at the end of each file name I want to put ": [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]". Lastly, I want to take that variable and place it in the "RecordOfChanges" text field. So for example, if the file names where Doc1.docx, Doc2.xlsx and Doc3.pdf, then the RecordOfChanges field should look like:

Doc1.docx: [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]

Doc2.xlsx: [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]

Doc3.pdf: [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]

Here is the code I have tried so far and I get an error

"Run-Time Error 451. property let procedure not defined and property get procedure did not return an object VBA".

The debugger highlights the line
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
1
  • What is subform's RecordSource? How does control show list of attachment filenames?
    – June7
    Dec 6, 2022 at 23:17

1 Answer 1

0

I don't think your approach of looping through attachments control is going to work (I tried). Open a recordset object and loop records. Probably need to first commit record to table.

Private Sub BtnEditSOW_Click()
Dim varChanges As String
Dim rs As DAO.Recordset

Me.Dirty = False 'force record to commit to table 
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 and build varChanges string.
    varChanges = varChanges & rs(0) & ": [Note changes to this attachment here. Put 'No Changes' if none. Or delete this line if not applicable.]" & _
                              Chr(13) & Chr(10)
    rs.MoveNext
Loop
'Put varChanges in the Record Of Changes text box control.
Me.Attachments_RecordOfChanges = varChanges
End Sub

Side note: Could use vbCrLf instead Chr(13) & Chr(10).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.