Results 1 to 8 of 8
  1. #1
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228

    Adding attachments to a table

    I am looking for a way to attach X amount of pictures to a record. I want to use a command button that opens an explorer to choose X amount of files. I don't want to just open a folder because that won't return the file location.



    Does anybody know how to open an "attachment explorer"?

    Access 2007

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Here are some VBA examples
    Attached Files Attached Files

  3. #3
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228
    I found basically the same code while googling this issue. Every time I run the code the variable "m_strFieldFileData" is empty. How does this variable become populated?

    Here is my code...

    Code:
    Private Sub cmdattach_Click()Dim claimnum As String, serialno As Double, rs As DAO.Recordset, db As DAO.Database
    
    
    claimnum = xx
    serialno = 1
    filepath = SelectFile()
    
    
    If filepath <> "" Then
        Set db = CurrentDb
        Set rs = db.OpenRecordset("Picture Table")
        With rs
            .AddNew
                AddAttachment rs, "Attach", filepath
                .Fields("Claim Number") = claimnum
                .Fields("Serial Number") = serialno
            .Update
            .Close
        End With
    End If
    
    
    End Sub
    My 2 public functions...

    Code:
    Public Function SelectFile() As String    Dim fd As Office.FileDialog
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .AllowMultiSelect = False
            .Title = "Please select file to attach"
            If .Show = True Then
                SelectFile = .SelectedItems(1)
            Else
                Exit Function
            End If
        End With
        Set fd = Nothing
    End Function
    
    Public Sub AddAttachment(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, ByVal strFilePath As String)
     
    On Error GoTo AddAttachment_Err
    Dim rstChild As DAO.Recordset2
    Dim fldAttach As DAO.Field2
    Set rstChild = rstCurrent.Fields(strFieldName).Value
    rstChild.AddNew
    Set fldAttach = rstChild.Fields(m_strFieldFileData)
    fldAttach.LoadFromFile strFilePath
     rstChild.Update
    rstChild.Close
     Exit Sub
     
    AddAttachment_Err:
    ' Run-time error '3820': (occurs if the file with the same name is already attached)
    If Err.Number <> 3820 Then
     modUtility.ErrorMessage
    Else
     VBA.Interaction.MsgBox "File already attached!", vbInformation + vbOKOnly, modConstant.AppName
    End If
     Resume AddAttachment_End
     
    AddAttachment_End:
    End Sub

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I provided a working example that you can compare to. Maybe you can step debug the example to get an illustration. That would probably be better than me trying to describe it here. Your m_strFieldFileData is not mentioned or declared anywhere in the code you provided. In order to get the whole thing to work, you need to nest two DAO recordsets, you would get the missing info from the parent recordset.

  5. #5
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228
    I just substituted m_strFieldFileData with "FileData" and it works for one attachment.

    However, now I am looking into attaching multiple files. I am passing an array of file URL's and everything runs, but it doesn't attach the multiple files. Only one.

    Code:
    Public Sub AddAttachment(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String, filenms() As Variant, cnt As Integer) 
    On Error GoTo AddAttachment_Err
    Dim rstChild As DAO.Recordset2
    
    
    Set rstChild = rstCurrent.Fields(strFieldName).Value
    
    
    For n = 0 To cnt
        rstChild.AddNew
        rstChild.Fields("FileData").LoadFromFile filenms(n)
    Next n
    
    
    rstChild.Update
    rstChild.Close
    Exit Sub
     
    AddAttachment_Err:
    ' Run-time error '3820': (occurs if the file with the same name is already attached)
    If Err.Number <> 3820 Then
     modUtility.ErrorMessage
    Else
     VBA.Interaction.MsgBox "File already attached!", vbInformation + vbOKOnly, modConstant.AppName
    End If
     Resume AddAttachment_End
     
    AddAttachment_End:
    End Sub
    How do I get it to add multiples?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You will need to successfully automate a single file before you can iterate and enumerate multiple files. Hardcoding a file name does not indicate to me that you have successfully automated the attach action.

  7. #7
    kdbailey is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Aug 2012
    Posts
    228
    How do I refer m_strFieldFileData to a file then? I could not find anything describing what it even is.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I suggest you step debug the sample I provided in post # 2. Follow the suggestions in post #4 to get your answer(s).

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Button to add attachments to a table
    By kdbailey in forum Access
    Replies: 1
    Last Post: 07-30-2014, 10:04 AM
  2. Replies: 8
    Last Post: 12-12-2013, 03:54 PM
  3. Replies: 5
    Last Post: 10-14-2013, 11:53 AM
  4. attachments and adding new records
    By chiefmsb in forum Access
    Replies: 0
    Last Post: 07-06-2011, 01:22 PM
  5. Replies: 0
    Last Post: 10-25-2010, 09:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums