Loop record attached document names

pike

New member
Local time
Tomorrow, 00:04
Joined
Feb 14, 2016
Messages
9
Hello,
currently using the function below to attach a word document to a record.
If a document already exist with same name it triggers the error number 3820
Code:
Public Sub loadAttachFromFile(strPath As String, rsAll As DAO.Recordset, attachmentFieldName As String)
    Dim rsAtt As DAO.Recordset
    On Error Resume Next
    Set rsAtt = rsAll.Fields(attachmentFieldName).Value
    rsAll.Edit
    rsAtt.AddNew
    rsAtt.Fields("FileData").LoadFromFile (strPath)
    rsAtt.Update
    rsAll.Update
    If Err.Number = 3820 Then
        On Error GoTo 0
        MsgBox "Parent letter already exists", vbOKOnly + vbInformation, "Letter Name Confilct"
    End If
End Sub

is there a way to loop all the attached documents in the record and find there name?
This way I could handle any potential naming conflict before it gets to the function and remove the "on error"
 
solved it with
Code:
Function AttachedLetters(strWordDocument As String) As Boolean
    Dim attItem
    If Me.Attachments.AttachmentCount > 0 Then
        For attItem = 0 To Me.Attachments.AttachmentCount - 1
            If Me.Attachments.FileName(attItem) Like strWordDocument Then
                AttachedLetters = True
                MsgBox "Letter to Parent already exists" & Chr(10) & strWordDocument, vbOKOnly + vbInformation, "Letter Naming Confilct"
                Exit For
            End If
        Next
    End If
End Function
 

Users who are viewing this thread

Back
Top Bottom