Results 1 to 15 of 15
  1. #1
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33

    Searching files in a predefined directory through a button

    Good evening everyone,


    I'm Beppe and this is my first thread here. I'm very new to Access, since I started just last week using it for work.

    I have a question for you all: in my access mask the user selects several fields in order to compile a string of characters. This string represents the name of a file stored in a directory.
    What I need to do is to search in a predefined directory and look if there's a file named like the user has previously selected. I thought to resolve this issue through a button that does this research...

    Can you help me with the code to be written in this button??

    Thank you in advance
    Beppe

  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,902
    Something like:

    If Dir("defined path" & Me.tbx1 & "\" & Me.tbx2) <> = "" Then MsgBox "File found"

    Show examples of data in the fields.
    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
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    The string produced by the user is for example:

    COM-CE7001AR0-N049-DAMMER-XXXX-XXXXXX-CAROPRESEX-170130-XXXX-XXXXXX-001-PASSIR-ACCE

    Tell me if you need more info...

    I tried with the code that you have attached, changing a bit because it gave me error, but the message "File found" comes even if the file does not exists..

    Code:
    If Dir("Z:\ArchivioN" & Me.Testo495) = "" Then
        MsgBox "File found"
    End If
    Could you help me?
    Thanks for everything

  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,902
    13 fields to construct a filename? Okay, concatenate.

    Dir("defined path" & [AAA] & "-" & [BBBBBBBBB] & "-" & [CLIENT CODE] & "-" & [CLIENT NAME] & "-" & [SUPPLIER CODE] & "-" & [XXXXXX] & "-" & [SUPPLIER NAME] & "-" & [REFERENCE PERSON] & "-" & [DATE] & "-" & [N° JOB] & "-" & [PROGRESSIVE N°] & "-" & [CITY] & "-" & [TYPE] & ".pdf"

    Use whatever file extension is appropriate.
    Last edited by June7; 09-18-2017 at 02:11 PM.
    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
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    Actually I concatenate all the fields in just one textbox called "Testo495", is it ok to recall just this?

    What about the If function in the command button?

    Thank you you're really helping

  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,902
    Certainly, if you have the fields concatenated in a textbox then just reference the textbox.

    What about the If? What do you not understand about the example?

    What do you want to happen after the search?
    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
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    I would like that the button does the following, with an If function:

    - search in the predefined directory if the file exists, then show mgb "File found" and opens directory (this works fine with Application.followhyperlink)
    - if the file does not exists shows mgb "File not found"

    I tried with this:
    Code:
    If Dir("Z:\ArchivioN" & Me.Testo495 & ".docx" & ".xlsx") = "" Then
        MsgBox "File found"
        Application.FollowHyperlink ("Z:\ArchivioN")
        else
        MsgBox "File not found"
        End If
    This shows anyway the mgb "File found" even if I change some fields in order to see if it does not find the file

  8. #8
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    Just to clarify, when I use the following code (which seems very logical to me) I always see the mgb "File not found"

    Code:
    If Dir("Z:\ArchivioN" & ".docx" & ".xlsx") = Me.Testo495 Then
    
    
       MsgBox "File found"
       Application.FollowHyperlink ("Z:\ArchivioN")
    
    
       Else
       MsgBox "File not found"
    
    
    End If

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Need a \ at the end of the defined path for the search. Maybe you have it in your code and the forum has dropped it. Have to go to post Advanced Edit and uncheck "Automatically parse links in text."

    A file name can have only 1 file extension. Use wildcard.

    If Dir("Z:\ArchivioN\" & Me.Testo495 & ".*") <> "" Then
    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.

  10. #10
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    I tried like you said:

    Code:
    If Dir("Z:\ArchivioN\" & Me.Testo495 & ".*" <> "") Then
        MsgBox "File found"
        Application.FollowHyperlink ("Z:\ArchivioN")
            Else
        MsgBox "File not found"
    End If
    but it gives me an error 13 "type mismatch", underlining the first If line..

    Thanks for the patience June7

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    That closing ) is in the wrong place.
    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.

  12. #12
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    IT WORKS!!

    Code:
    If Dir("Z:\ArchivioN\" & Me.Testo495 & ".*") <> "" Then
        MsgBox "File found"
        Application.FollowHyperlink ("Z:\ArchivioN\")
        
        Else
        MsgBox "File not found"
        
    End If
    I really thank you June7!!!

    Best regards
    Beppe

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Suggest you don't bother user with the 'File Found' message - just open the folder.
    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.

  14. #14
    Beppe is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    33
    Done!
    Can you explain me the logic of the first line?


    Just to clarify: how I change the status of this post in "solved"? I see it changed

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    If the Dir() function does not find a match it returns an empty string.


    Thread Tools dropdown above first post. Any poster in a thread can change thread status.
    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. list files in a directory
    By alfrval in forum Access
    Replies: 2
    Last Post: 02-25-2015, 12:46 PM
  2. list all files in a directory
    By snipe in forum Programming
    Replies: 5
    Last Post: 01-21-2014, 12:18 PM
  3. Replies: 6
    Last Post: 10-26-2012, 12:53 PM
  4. Replies: 6
    Last Post: 06-15-2011, 04:38 PM
  5. attachments to predefined directory
    By surfcaster in forum Forms
    Replies: 0
    Last Post: 03-04-2010, 02:22 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