Results 1 to 7 of 7
  1. #1
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14

    Using file names from a listbox and .LoadFromFile Method to upload attachments to a table


    Good Afternoon,

    I'm trying to loop through the values in a listbox (called "Attachments") and use those values to attach a file to a new record in a table. The attachments field in the table is called "Evidence". The below code will create a new record but not add the attachments.
    Code:
    Private Sub cmdImport_Click()
        Dim dbs As Database
        Dim rst As Recordset
        Dim aFile
        
        Set dbs = CurrentDb()
        Set rst = dbs.OpenRecordset("VRM Evidence")
    
        rst.Edit
        rst.AddNew
        For Each aFile In Me.Attachments.ItemsSelected
            rst.Fields("Evidence").LoadFromFile Me.Attachments.ItemData(aFile)
        Next
        rst.Update
        
        rst.Close
        Set rst = Nothing
        Set dbs = Nothing
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Attachment data type is a type of multi-value field. Adding data to a multi-value field requires opening a 'child' recordset. http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    Lots of large embedded documents will use up Access 2GB file size limit fast.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14
    Ok, I've been trying to get my head around this and it just isn't working. Below is the code I'm using:

    Code:
    Private Sub cmdImport_Click()
        Dim dbs As Database
        Dim rst As Recordset
        Dim arst As Recordset2
        Dim aFile
        
        Set dbs = CurrentDb()
        Set rst = dbs.OpenRecordset("VRM Evidence")
        
        Set arst = rst.Fields(“Evidence”).Value
    On Error Resume Next
    For Each aFile In Me.Attachments
        With arst
            .AddNew
            .Fields(“FileData”).LoadFromFile (Me.Attachments.ItemData(aFile))
            .Update
            .Close
        End With
    Next
        rst.Update
        
        Set rst = Nothing
        Set dbs = Nothing
    End Sub
    • I have a table called "VRM Evidence" with an attachments field called "Evidence"
    • On the entry form I have a "Browse" button that has code that allows the user to search for 1 or more files and the file path(s) are populated into a list box on the form called "Attachments"
    • When the Import button is clicked, it's supposed to:
      • create a new record on the "VRM Evidence" table
      • using the .LoadFromFile method, add the attachments based on the file paths from the listbox


    This code is neither creating a new record nor adding the attachments and I can't figure out why...

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Need to set the parent recordset to edit mode:
    rst.Edit

    Code to loop through all items of listbox:
    http://stackoverflow.com/questions/2...a-list-box-vba

    Suggest you remove the On Error Resume Next line because this interferes with debugging.
    Code:
    Private Sub cmdImport_Click()
        Dim rst As Recordset
        Dim arst As Recordset2
        Dim i As Integer
        Set rst = CurrentDb.OpenRecordset("VRM Evidence")
        rst.Edit
        Set arst = rst.Fields("Evidence").Value
        For i = 0 To Me.Attachment.ListCount - 1
            arst.AddNew
            arst.Fields("FileData").LoadFromFile (Me.Attachment.ItemData(i))
            arst.Update
        Next i
        rst.Update
    End Sub
    It only saves attachment to the first record of rst.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14
    This was very helpful thanks! I have updated the code. Now I'm getting an actual error:

    Error 3265
    Item not found in this collection

    Code:
    Private Sub cmdImport_Click()
    Dim dbs As Database
    Dim rst As Recordset
    Dim arst As Recordset2
    
    Dim aFile
    
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("VRM Evidence")
    
    rst.Edit
    
    Set arst = rst.Fields("Evidence").Value
    
    On Error GoTo Proc_Error
    
    For aFile = 0 To Me.Attachments.ListCount - 1
    With arst
    .AddNew .Fields(“FileData”).LoadFromFile (Me.Attachments.ItemData(aFile)) .Update .Close
    End With
    Next rst.Update Set rst = Nothing Set dbs = Nothing Proc_Exit: Exit Sub Proc_Error: MsgBox "Error " & Err.Number & " in cmdImport.Click" & vbCrLf & Err.Description Resume Proc_Exit End Sub
    The line that is producing the error is .Fields(“FileData”).LoadFromFile (Me.Attachments.ItemData(aFile))

    I also am not sure what you mean by
    It only saves attachment to the first record of rst.
    I thought this would create a new record and attach the file to that? If that is not the case is there a better way to do this?

  6. #6
    PoolHallJunkie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2014
    Posts
    14
    Hi! I fixed the error, I wanted to post the final code for reference if others should look this up:

    Code:
    Private Sub cmdImport_Click()
        Dim dbs As Database
        Dim rst As Recordset2
        Dim arst As Recordset2
        Dim fld As Field2
        
        Dim aFile
        
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("VRM Evidence")
        Set fld = rst("Evidence")
        
        rst.Edit
            
        Set arst = fld.Value
    On Error GoTo Proc_Error
    For aFile = 0 To Me.Attachments.ListCount - 1
        With arst
            .AddNew
            .Fields("FileData").LoadFromFile (Me.Attachments.ItemData(aFile))
            .Update
            .Close
        End With
    Next
        rst.Update
        
        Set rst = Nothing
        Set dbs = Nothing
        
    Proc_Exit:
      Exit Sub
    Proc_Error:
      MsgBox "Error " & Err.Number & " in cmdImport.Click" & vbCrLf & Err.Description
      Resume Proc_Exit
    
    End Sub
    You are correct though, it only attaches the document to the 1st record of the table. This is a different issue however so I will start a new thread. This particular issue is solved. Thank you very much for your help June7, you're pretty much a life saver.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    If you want a new parent record then change rst.Edit to rst.AddNew.

    Then you probably need to populate other fields of the parent record as well.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. upload a file
    By newbieaccess in forum Access
    Replies: 1
    Last Post: 08-24-2014, 09:41 AM
  2. Replies: 7
    Last Post: 08-20-2014, 03:00 AM
  3. Replies: 2
    Last Post: 11-06-2013, 10:43 AM
  4. Importing file names into a table?
    By davetheant in forum Forms
    Replies: 6
    Last Post: 06-10-2011, 10:37 PM
  5. Upload file inside .mdb database file
    By havish in forum Access
    Replies: 0
    Last Post: 07-30-2008, 07:35 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