Results 1 to 10 of 10
  1. #1
    ahmadrezaahmad is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2013
    Posts
    14

    Problem with append query for attachment field

    I have 1 "main" access file and "Portable".
    in the form of main I creat buttom to open and apped the table of other access file-portable.accdb- to the main table!
    I have a problem, it is that when I add field content of attachment to the main table I can not use "Append" buttom !
    attach:
    1-befor adding attachmend field=ok!
    2-after adding attachment field=ERRROR!!!!
    Please help me!
    Thank you

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Cannot append attachment field with query. Requires VBA code to save the file(s) to external folder then save external files back into new record. Review http://blogs.office.com/b/microsoft-...cess-2007.aspx

    Embedding files in table chews up file limit. Access has a 2gb size limit.
    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
    ahmadrezaahmad is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2013
    Posts
    14
    Quote Originally Posted by June7 View Post
    Requires VBA code to save the file(s) to external folder then save external files back into new record. Review http://blogs.office.com/b/microsoft-...cess-2007.aspx
    this code for one file and I had seen this code!
    but my problem is appendin from one db to another db!
    for example in my file ,main db is (main.accdb) and other persons fill forms in other db (portable.acccdb),I want to append forms that recieved from persons in many file as (portable.accdb) to one main file=main.accdb!
    for this sitution what your idea???(pls See attachmentS)
    thanks!
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Adapt code. The process is basically the same. Open query of source table in 'portable' db. Open query of destination table in 'main' db. Save attachment from source to a folder. Save file from folder into destination attachment.

    Data from the four PK fields will have to be saved in a separate SQL action then run code to move the attachment data.

    The [to] field is not a unique value and therefore appears not useful as criteria in the query. Seems would need all four fields of the compound PK. Don't understand what the data represents. The [time] field is number type set as Long Integer but input mask is yyyy/mm/ddd. The date displays like 1392/04/04 which is surely not a valid date. Date values should be stored in a date/time field type. Should not use [from], [to], [time] as field names. One or all of them cause the INSERT sql to fail if the [] are omitted. Also, describtion is a misspelling of description.

    Here is code distilled from the referenced links that works for me.
    Code:
    Sub btnAppend_Click()
    Dim strExtract As String
    Dim dbs As DAO.Database
    Dim rstSource As DAO.Recordset
    Dim rstSourceChild As DAO.Recordset
    Dim rstDest As DAO.Recordset
    Dim rstDestChild As DAO.Recordset
    Dim strFilePath As String
    strExtract = "C:\Temp\Portable.accdb"
    CurrentDb.Execute "INSERT INTO asli([from], [to], [describtion], [time]) SELECT [from], [to], [describtion], [time] FROM [" & strExtract & "].asli;"
    Set dbs = OpenDatabase("C:\Temp\Portable.accdb")
    Set rstSource = dbs.OpenRecordset("SELECT * FROM asli;", dbOpenDynaset)
    While Not rstSource.EOF
        Set rstSourceChild = rstSource.Fields("attach").Value
        If Not rstSourceChild.EOF Then
            Set rstDest = CurrentDb.OpenRecordset("SELECT * FROM asli WHERE [from]='" & rstSource![from] & _
                "' AND [to]='" & rstSource![to] & _
                "' AND describtion='" & rstSource!describtion & _
                "' AND [time]=" & rstSource![Time], dbOpenDynaset)
            Set rstDestChild = rstDest.Fields("attach").Value
            rstDest.Edit
            While Not rstSourceChild.EOF
                'save attachment from Portable to folder
                strFilePath = "C:\Temp\" & rstSourceChild.Fields("FileName")
                If Dir(strFilePath) <> "" Then VBA.Kill strFilePath
                rstSourceChild.Fields("FileData").SaveToFile strFilePath
                'save attachment from folder to Main
                rstDestChild.AddNew
                rstDestChild.Fields("FileData").LoadFromFile strFilePath ' store the file's contents in the new row.
                rstDestChild.Update ' commit the new row.
                rstSourceChild.MoveNext
            Wend
            rstSourceChild.Close
            rstDestChild.Close
            rstDest.Update
            rstDest.Close
        End If
        rstSource.MoveNext
    Wend
    End Sub
    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
    ahmadrezaahmad is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2013
    Posts
    14
    thanks a lot..
    I try to adapt your code to my file but I have error..
    can yo attach your "ok" file to me?
    I like my code is general like "befor adding attaments field" file and not depend on file folder like your coed:

    strExtract = "c:\Temp\Portable.accdb"

    can you help me with attaching true file

    tnx again!

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    What is the error?

    All I did was replace the button code and run it. Did not have to change anything else. The following will allow user to select source file path with the file dialog. Get this to work as is before trying more customization.
    Code:
    Dim dbs As DAO.Database
    Dim rstSource As DAO.Recordset
    Dim rstSourceChild As DAO.Recordset
    Dim rstDest As DAO.Recordset
    Dim rstDestChild As DAO.Recordset
    Dim strSql As String, fd As FileDialog
    Dim strFilePath As String, strExtract As String
    Set fd = FileDialog(msoFileDialogFilePicker)
    With fd
        .AllowMultiSelect = False
        .Filters.Add "Access Files", "*.mdb;*.accdb", 1
        .InitialFileName = CurrentProject.Path
        If .Show Then strExtract = .SelectedItems(1)
    End With
    If strExtract <> "" Then
        If MsgBox("REWRITE?", vbYesNo + vbQuestion) = vbYes Then
            CurrentDb.Execute "DELETE FROM asli"
        End If
        
        CurrentDb.Execute "INSERT INTO asli([from], [to], describtion, [time]) " & _
                            "SELECT [from], [to], describtion, [time] FROM " & strExtract & ".asli"
        
        Set dbs = OpenDatabase(strExtract)
        Set rstSource = dbs.OpenRecordset("SELECT * FROM asli;", dbOpenDynaset)
        While Not rstSource.EOF
            Set rstSourceChild = rstSource.Fields("attach").Value
            If Not rstSourceChild.EOF Then
                Set rstDest = CurrentDb.OpenRecordset("SELECT * FROM asli WHERE from='" & rstSource![from] & _
                    "' AND to='" & rstSource!to & _
                    "' AND describtion='" & rstSource!describtion & _
                    "' AND time=" & rstSource!Time, dbOpenDynaset)
                Set rstDestChild = rstDest.Fields("attach").Value
                rstDest.Edit
                While Not rstSourceChild.EOF
                    'save attachment from Portable to folder
                    strFilePath = "C:\Temp\" & rstSourceChild.Fields("FileName")
                    If Dir(strFilePath) <> "" Then VBA.Kill strFilePath
                    rstSourceChild.Fields("FileData").SaveToFile strFilePath
                    'save attachment from folder to Main
                    rstDestChild.AddNew
                    rstDestChild.Fields("FileData").LoadFromFile strFilePath ' store the file's contents in the new row.
                    rstDestChild.Update ' commit the new row.
                    rstSourceChild.MoveNext
                Wend
                rstSourceChild.Close
                rstDestChild.Close
                rstDest.Update
                rstDest.Close
            End If
            rstSource.MoveNext
        Wend
    End If
    Last edited by June7; 06-29-2013 at 12:10 AM.
    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.

  7. #7
    ahmadrezaahmad is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2013
    Posts
    14
    hi
    unfrotunatly your 2nd code fail too!!
    I attached 2 Error Screen shots!
    pls attach your "OK" access file!!!
    I import my module to chechk it!!
    tnx
    Attached Thumbnails Attached Thumbnails Untitled.jpg  
    Attached Files Attached Files

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    This is weird. I wrote and tested that code in Access 2007 which worked. I just tested in Access 2010 and it doesn't work. Don't understand why.
    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.

  9. #9
    ahmadrezaahmad is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Jun 2013
    Posts
    14
    please upload your accdb file....
    please....

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Attaching db with that code won't do you any good. See my previous post. The code isn't working for Access 2010/2013.

    The only alternative I can see is to set a link to the asli table in Portable. In my test I call the link asliPortable. Then this code works to copy records from asliPortable to asli. Use the Import/Export wizard to set the link then copy/paste this code into the button Click event.
    Code:
    Dim rstSource As DAO.Recordset
    Dim rstSourceChild As DAO.Recordset
    Dim rstDest As DAO.Recordset
    Dim rstDestChild As DAO.Recordset
    Dim strFilePath As String, strExtract As String
    If MsgBox("REWRITE?", vbYesNo + vbQuestion) = vbYes Then
        CurrentDb.Execute "DELETE FROM asli"
    End If
    CurrentDb.Execute "INSERT INTO asli([from], [to], describtion, [time]) " & _
                        "SELECT [from], [to], describtion, [time] FROM asliPortable"
    Set rstSource = CurrentDb.OpenRecordset("SELECT * FROM asliPortable;", dbOpenDynaset)
    While Not rstSource.EOF
        Set rstSourceChild = rstSource.Fields("attach").Value
        If Not rstSourceChild.EOF Then
            Set rstDest = CurrentDb.OpenRecordset("SELECT * FROM asli WHERE from='" & rstSource![from] & _
                "' AND to='" & rstSource!to & _
                "' AND describtion='" & rstSource!describtion & _
                "' AND time=" & rstSource!Time, dbOpenDynaset)
            Set rstDestChild = rstDest.Fields("attach").Value
            rstDest.Edit
            While Not rstSourceChild.EOF
                'save attachment from Portable to folder
                strFilePath = "C:\Users\June\Forums\" & rstSourceChild.Fields("FileName")
                If Dir(strFilePath) <> "" Then VBA.Kill strFilePath
                rstSourceChild.Fields("FileData").SaveToFile strFilePath
                'save attachment from folder to Main
                rstDestChild.AddNew
                rstDestChild.Fields("FileData").LoadFromFile strFilePath ' store the file's contents in the new row.
                rstDestChild.Update ' commit the new row.
                rstSourceChild.MoveNext
                Kill strFilePath
            Wend
            rstSourceChild.Close
            rstDestChild.Close
            rstDest.Update
            rstDest.Close
        End If
        rstSource.MoveNext
    Wend
    Last edited by June7; 06-30-2013 at 01:25 AM.
    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. Append Query - one field distinct
    By Rhubie in forum Queries
    Replies: 1
    Last Post: 03-28-2013, 11:46 AM
  2. Linked Table Append Query Problem.
    By dlab85 in forum Queries
    Replies: 4
    Last Post: 03-13-2013, 07:30 AM
  3. Using Update Query with Attachment Field
    By Emily.G in forum Queries
    Replies: 4
    Last Post: 08-17-2012, 07:37 AM
  4. Replies: 16
    Last Post: 04-30-2012, 07:12 AM
  5. append query: dlookup or dmax problem
    By jeffr27 in forum Queries
    Replies: 1
    Last Post: 05-10-2011, 09:25 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