Consulting

Results 1 to 4 of 4

Thread: Does published drive in a network exists?

  1. #1
    VBAX Contributor
    Joined
    Oct 2011
    Location
    Concord, California
    Posts
    101
    Location

    Does published drive in a network exists?

    I backup my back-end database to a computer's published drive on a network. Sometimes, however, this computer is off-line and therefore my backup procedure fails because the specified path does not exists.

    This is the backup path: \\DELL800W7\Data2\Backups\MiltonHPlaptop

    I know how to check if a drive letter exists, i.e. D:,E:, F: but can't seem to get code to verify \\DELL800W7\Data2\Backups\MiltonHPlaptop path.
    This is the code I'm using to find out if a drive exists:
    Public Function f_DriveExists(DirectoryPath As String) As Boolean    Dim objFS As Object
        On Error GoTo ErrorTrap
        'Assume it does not
        f_DriveExists = False
        
        Set objFS = CreateObject("Scripting.FileSystemObject")
        'DriveExists returns True or False
        f_DriveExists = objFS.DriveExists(DirectoryPath)
    ExitPoint:
        Exit Function
    ErrorTrap:
        MsgBox "f_DriveExists: " & Err.Description, vbExclamation, "Error"
        f_DriveExists = False
        GoTo ExitPoint
    End Function

  2. #2
    You can use the built in VBA DIR() function to do this.

    Here is an example UDF:


    Option Compare Database
    Option Explicit
    
    Public Function fFolderExists(strFullPath As String) As Boolean
    'Author       : Boyd Trimmell aka HiTechCoach at http://www.hitechcoach.com
    'Purpose: Check if a folder exists
    ' Returns: True or False
        
    Dim strFolderPath As String
    
        strFolderPath = strFullPath
        
        ' make sure folder path ends with a \
        If Right(strFolderPath, 1) <> "\" Then strFolderPath = strFolderPath & "\"
        
        On Error Resume Next
        If Not Dir(strFolderPath, vbDirectory) = vbNullString Then fFolderExists = True
        
    
    End Function

    Example usage in the immediate windows:

    ? fFolderExists("\\DELL800W7\Data2\Backups\MiltonHPlaptop")
    Boyd Trimmell aka HiTechCoach
    Microsoft Access MVP -2010-2015

    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

  3. #3
    VBAX Contributor
    Joined
    Oct 2011
    Location
    Concord, California
    Posts
    101
    Location
    Hi HiTechCoach:

    Thanks for your suggestion. I tried your code with variations of folder paths (\\somedriveIknowDoesNotExist, \myDrive) and the code although it run, it reported it had found the directory, which I knew did not exist.

  4. #4
    Oops ... Here is code that will handle UNC paths

    Public Function fFolderExists(strFullPath As String) As Boolean
    'Author       : Boyd Trimmell aka HiTechCoach at http://www.hitechcoach.com
    'Purpose: Check if a folder exists
        
    Dim strFolderPath As String
    
        strFolderPath = strFullPath
        
        ' make sure folder path ends with a \
        If Right(strFolderPath, 1) <> "\" Then strFolderPath = strFolderPath & "\"
        
        On Error GoTo Err_handler
        If Not Dir(strFolderPath, vbDirectory) = vbNullString Then fFolderExists = True
        
        
        Exit Function
        
    Err_handler:
        If Err.Number <> 52 Then MsgBox Err.Number & " " & Err.Description
    
    End Function
    Boyd Trimmell aka HiTechCoach
    Microsoft Access MVP -2010-2015

    Programming: Nine different ways to do it right, a thousand ways to do it wrong.
    Binary--it's as easy as 1-10-11

Posting Permissions

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