Results 1 to 14 of 14
  1. #1
    aspen is offline Competent Performer
    Windows Vista Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    127

    add files to attachment field from a folder without dubulcates


    Hi every one

    I have table with an attachment field and on my form i have put a code on the click event of the id field. here is the code

    Const strRootFolder As String = "C:\Users\eve\Desktop\New\"
    AddAttachmentsFromFolder strRootFolder, "Table1", "Files", _
    "*.*", True
    MsgBox "Done adding files from: " & vbCrLf & strRootFolder, _
    vbInformation, "File Import Process Completed"

    This code ads all the files in the foldder. what i want acomplish is to change the code so that it would not add a file if it already exists in the the table. I am very at vba so can some one please help me.

    Thank you

  2. #2
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Something along these lines :

    if FileExists("C:\Users\eve\Desktop\New\" & me.AttachmentField & ") = true then
    AddAttachmentsFromFolder strRootFolder, "Table1", "Files", _
    " & me.AttachmentField & ", False
    else
    'nothing else
    end if
    exit sub

    Its aircode but ill try to test it for you

  3. #3
    aspen is offline Competent Performer
    Windows Vista Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    127
    I am using this module along with that code . please try
    Code:
    ' -------------------------------------------------------------------------
    ' Procedure : AddAttachmentsFromFolder
    ' Purpose : Adds one record to an attachment field for each file in the
    ' : specified folder.
    ' Arguments : strFolder - Name of the folder
    ' : strTable - Name of the table containing the attachment field
    ' : strField - Name of the attachment field
    ' : strPattern - Search pattern for the directory. Defaults to
    ' : all files (*.*)
    ' : fIncludeSubfolders - Include subfolders in the specified folder
    ' : Defaults to False
    ' Comments : This routine adds a new record to the table, then one attachment.
    ' : It commits the changes and handles all cleanup
    ' -------------------------------------------------------------------------
    Sub AddAttachmentsFromFolder(ByVal strFolder As String, _
    ByVal strTable As String, _
    ByVal strField As String, _
    Optional ByVal strPattern As String = "*.*", _
    Optional ByVal fIncludeSubfolders As Boolean = False)
    Dim strFile As String
    Dim lngCount As Long
    Dim rstParent As DAO.Recordset2
    Dim rstChild As DAO.Recordset
    Dim fldAttach As DAO.Field2
    Dim objFso As Object ' Scripting.FileSystemObject
    Dim objFolder As Object ' Scripting.Folder
    Dim objFile As Object ' Scripting.File
    Dim objSubFolder As Object ' Scripting.Folder
    
    On Error GoTo ErrorHandler
    
    ' Instantiate the FileSystemObject
    Set objFso = CreateObject("Scripting.FileSystemObject")
    ' fix up the folder
    
    If (Right(strFolder, 1) <> "\") Then strFolder = strFolder & "\"
    ' make sure the folder exists
    If (Dir(strFolder, vbDirectory) = "") Then
       MsgBox "The specified folder does not exist: " & strFolder, vbExclamation
       Exit Sub
    End If
    ' get the folder object
    Set objFolder = objFso.GetFolder(strFolder)
    ' open the table containing the attachment field
    Set rstParent = CurrentDb().OpenRecordset(strTable)
    ' get the first file
    strFile = Dir(strFolder & strPattern)
    ' get each file that meets the pattern
    While (Len(strFile) > 0)
       ' add a record to the parent table
       Debug.Print strFolder & strFile
       rstParent.AddNew
       ' get the attachment recordset and FileData field to contain the file
       Set rstChild = rstParent.Fields(strField).Value
       Set fldAttach = rstChild.Fields("FileData")
       ' add the attachment to the attachment field
       rstChild.AddNew
       fldAttach.LoadFromFile strFolder & strFile
       rstChild.Update
       rstParent.Update
       ' get the next file
       strFile = Dir
    Wend
    ' recurse subfolders?
    If (fIncludeSubfolders) Then
       For Each objSubFolder In objFolder.SubFolders
          AddAttachmentsFromFolder objSubFolder.Path, strTable, strField, _
          strPattern, fIncludeSubfolders
       Next
    End If
    Cleanup:
       rstParent.close
       Set rstParent = Nothing
    Exit Sub
    ErrorHandler:
       Debug.Print "Error " & Err.Number & " - " & Err.Description
       MsgBox Err.Description & vbCrLf & _
       Err.Number & vbCrLf & _
       Err.Source, VbMsgBoxStyle.vbCritical, "AddAttachmentsFromFolder Failed"
       GoTo Cleanup
    Thank you
    Last edited by June7; 05-17-2012 at 12:21 PM.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Use code tags as shown in my edit of your post. Use indentation in your code and it will be preserved when you paste between code tags.

    Checking to see if file already in the attachment field I think will involve opening a recordset of the attachment subtable. This http://msdn.microsoft.com/en-us/library/ff835669.aspx shows relevent code. Look at the procedure to save attachment to disc. Instead of doing the save within the loop, do a comparison of file names. If the file name is found, don't attach file, if not found then attach.
    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
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Right, ive done some testing and ive got it partially figured out.

    I added a .jpg file to a directory and let access check if it existed. When i checked it, it DID exist.
    When i removed the file and checked the existence again, it did NOT exist.
    This makes me believe that ive allmost got it figured out.

    Why allmost ?
    Because i didnt add files with your code. I looked for a specific file and i knew it was there and what its name was.
    Now, ill have to code it so it does that automaticly, so bare with me.

    To give you a heads up, here is my source (who else then Allen Browne, the Genius )
    http://allenbrowne.com/func-11.html

    I implemented his fileExist in the following code :

    Code:
    Private Sub knpAddFiles_Click()
    If FileExists("I:\Jeroen\MS-access\Afbeeldingen\Facillity.jpg") Then
    MsgBox "it exists"
    Else
    MsgBox "it does NOT exist"
    End If
    End Sub

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    JeroenMioch,

    aspen's requirement is to check if the file is already in the attachment field, not if it is in the directory.
    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
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Thats exactly what it does June7.

    It checks the attachement field, then looks in the directory and "tells" me that the file allready is there
    The only problem with further testing is that Aspens module to attache files doesnt work for me. So ill have to figure that out first.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    "6 of one, half a dozen of the other"

    How will you plan to dynamically pull the file names from the attachment field to check against files in directory? Still think need recordset of the attachment subtable.
    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
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    That is indeed the tricky part.
    But the first step has been achieved and that is to see if there are conflicting file names.

    My shift here at work is allmost at an end so ill try to think that over tomorrow.
    In the mean time if you have better suggestions then by all means post it. the purpose here is to help fellow programmers. So if its you or me finding the solution is irrelevant.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I am actually hoping aspen () will use the example in the link I provided along with my instructions and attempt some code for us to analyse.
    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.

  11. #11
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Right, somehow i missed your post haha.
    I hope it works for him, else we will look further

  12. #12
    aspen is offline Competent Performer
    Windows Vista Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    127
    Hi guys
    Thanks for your concern. it shows the forum is wild alive. i'll check the link first. i thought it would take some time for a response.

    Thank you

  13. #13
    aspen is offline Competent Performer
    Windows Vista Access 2010 64bit
    Join Date
    Jun 2011
    Posts
    127
    Hi every body
    I tried the code and the link. but my vba is enough. I know that code exists. But i cant pay the prize now. I got to stop some losses. This is for my kids. But i have to come to this latter.
    I am sory friend. I will come back another time. to this thread or a new thread . but i promise the same topic. I must fly now

    Bye

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    aspen, I looked again at the tutorial blog referenced in your other threads http://blogs.office.com/b/microsoft-...cess-2007.aspx

    The first example procedure uses error handler to deal with the possibility of duplicate attachment. However, this is only for a single document. In your case of attaching multiple documents from a folder, would not want to exit the sub but instead continue with the next document in folder. Most error handlers are designed to offer a graceful exit from a procedure when error encountered. Review for error handler example http://allenbrowne.com/ser-23a.html

    However, code execution can be returned to the primary code segment from the error. Review
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    http://www.ozgrid.com/forum/showthread.php?t=114989
    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. Replies: 16
    Last Post: 04-30-2012, 07:12 AM
  2. Copy files into specific folder
    By Jan22 in forum Access
    Replies: 12
    Last Post: 04-17-2012, 08:27 AM
  3. Replies: 2
    Last Post: 10-26-2011, 02:48 PM
  4. Replies: 9
    Last Post: 05-30-2011, 12:08 PM
  5. show filters files form a Folder
    By jimmyp75 in forum Programming
    Replies: 5
    Last Post: 03-16-2011, 12:46 PM

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