Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    See if a directory exists (Access XP)

    Is there a way to determine if a directory exists?

    I have tried:
    If Dir(strPath & IIf(Right(strPath, 1) = "", "", ""), vbDirectory) = "" Then

    What I get when the code is executed is:
    Run time error 52
    Bad file name or number

  2. #2
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: See if a directory exists (Access XP)

    Public Function DirectoryExists(Dir As String) As Boolean
    Dim oDir As New Scripting.FileSystemObject
    DirectoryExists = oDir.FolderExists(Dir)
    End Function

    found at http://www.freevbcode.com/ShowCode.asp?ID=29
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  3. #3
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: See if a directory exists (Access XP)

    Here is another way to do it.

    If Dir(strFoldername, vbDirectory) = "" Then
    ' folder does not exist
    Regards
    John



  4. #4
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    Thanks for your help, but what do I need to tick in references to make it work?

  5. #5
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    That is the problem I am having. Have another look at my first post.

  6. #6
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Emerald Beach, New South Wales, Australia
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: See if a directory exists (Access XP)

    Patt,

    try adding a reference to

    Windows Script Host Object Model which = windirsystem32wshom.ocx

    That should then work a treat.

    Stewart

  7. #7
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Emerald Beach, New South Wales, Australia
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: See if a directory exists (Access XP)

    Patt,

    you could also try

    Public Function IsPathValid(ByVal strUNCPath As String) As Boolean
    'COMMENTS:
    '
    ' - Determine whether the specified path exists.


    Dim strFolder As String 'Buffer for current folder.

    On Error GoTo ERRHandler

    strFolder = CurDir()

    ChDir strUNCPath
    ChDir strFolder

    IsPathValid = True

    Exit Function

    ERRHandler:

    IsPathValid = False
    ChDir strFolder

    Exit Function

    End Function 'IsPathValid()

  8. #8
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    The problem I am having is a compile error, User type not defined on the following line:

    Dim oDir As New Scripting.FileSystemObject

  9. #9
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    <P ID="edit" class=small>(Edited by patt on 16-Jun-04 18:06. correction)</P>What does the ChDir command do? I know what this does now.

  10. #10
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    Thanks Stewart, that works well.

  11. #11
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: See if a directory exists (Access XP)

    Sorry Pat

    I didn't read carefully enough.

    What happens if you just use:

    If Dir(strPath, vbDirectory) = "" Then

    In my testing, it does not seem necessary to add a "" to the end.
    Regards
    John



  12. #12
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    Don't be sorry, I do that as well, I read things in too much of a hurry sometimes and regret it later.

    Your suggestion here is what I originally had and it keeps coming up with "Bad filename or number".
    I thought originally it must have been a reference I had not declared.

    Thanks for your post, they all help.

  13. #13
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: See if a directory exists (Access XP)

    For those of us who remember DOS, there is an easy way. Every directory has a NUL component, so you merely have to look for NUL using your DIR function. I've modified your code below, the function will return NUL if the directory exists:

    If Dir(strPath & IIf(Right(strPath, 1) = "", "", "") & "NUL") = "NUL" Then
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  14. #14
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: See if a directory exists (Access XP)

    I have finally managed to produce the same error as you by putting something completely inappropriate in strpath.

    Perhaps you might check what is in it before testing with dir, by putting msgbox(strpath).

    I get the error if the path uses a full UNC path, but don't follow the computer name with a drive letter.

    eg strpath = "Johncabdos" gives that error, but
    strpath = "Johncdos" does not.
    Regards
    John



  15. #15
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: See if a directory exists (Access XP)

    No, that does not work where the directory does not exist, your code thinks that the directory exists.

    . I have the situation where I develop the system on my d: drive then copy it for a client who operated it on his c: drive. I have paths setup in a table which I use. If I don't change these from d:... to c:... I get the error.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •