Results 1 to 8 of 8
  1. #1
    Grant Shea is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    30

    Attach File using VBA

    I have a simple form that calls open a specific record and I would like to save a file from my computer to that records attachment field by using the dialog box. Can someone help me with the code for this? I found this code but i dont want to add a new row i want to add the file to an existing record that i have isolated using a query.

    Code:
     Dim dbs As DAO.databaseDim rst As DAO.RecordSet
    
        ' Ask the user for the file
        Dim filepath AsString
        filepath = SelectFile()
    
        ' Check that the user selected something
        If Len(filepath)=0Then
            Debug.Assert "No file selected!"
            ExitSub
        EndIf
    
        Set dbs = CurrentDb
        Set rst = dbs.OpenRecordset("tblAttach")
    
        ' Add a new row and an attachment
        rst.AddNew
        AddAttachment rst,"Files", filepath
        rst.Update
    
        ' Close the recordset
        rst.Close
        Set rst =Nothing
        Set dbs =Nothing


  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Here is some example code
    http://www.access-programmers.co.uk/...d.php?t=204915

    Here is an example that I created to help various people with their questions. So it is a kludge of working examples.
    Attached Files Attached Files

  3. #3
    Grant Shea is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    30
    Not quite understanding that...

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    Why are you storing the file in your database? is there a need to store different versions or original versions that you want to be uncorrupted? Storing documents in your database itself is a HUGE tax on the space your database takes up, you are far better off storing the path/file name and using something like followhyperlink to open the documents. If you anticipate your files will move folders frequently and the path to the file may change or get deleted etc that may be a reason to attach them but just beware it will bloat your database to do so quite quickly depending on how frequently it's used.

  5. #5
    Grant Shea is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    30
    it is not used often and it is for storing original versions that i want uncorrupted.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Grant Shea View Post
    Not quite understanding that...
    The hyperlink I provided will open a new browser window and navigate to a thread where this is a discussion on multiple value fields. The file I attached can be downloaded, extracted, and opened to view working examples of code to attach and extract files.

    As mentioned by rpeare, often times, it is a bad idea to use the attachment field. If you need to show a preview of a file within one of your forms or reports, you might want ot consider an Unbound OLE control.

  7. #7
    Grant Shea is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    30
    Ok so lets say i get rid of the multivalue field and use a followhyperlink. How can i set this up so when the user needs to add a file the hyperlink directs correctly.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I believe the example DB in post #2 use the Microsoft Office Object library to demonstrate a filepicker. This process will assign the path to a string variable that can be stored as text in a table.

    This post demonstrates the use of Application.FileDialog.
    https://www.accessforums.net/showthr...060#post229060
    You need to reference the correct library
    https://www.accessforums.net/showthr...072#post229072

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

Similar Threads

  1. attach more than one file
    By josros60 in forum Programming
    Replies: 1
    Last Post: 05-29-2016, 06:47 PM
  2. Replies: 3
    Last Post: 06-25-2015, 07:05 AM
  3. How to attach PDF file to an Access database
    By Ayiramala in forum Access
    Replies: 4
    Last Post: 01-09-2015, 11:59 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