PDA

View Full Version : [SOLVED:] Does published drive in a network exists?



mrojas
12-04-2013, 06:28 AM
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. :banghead:
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

HiTechCoach
12-05-2013, 02:51 AM
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")

mrojas
12-05-2013, 09:28 AM
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.

HiTechCoach
12-05-2013, 11:01 AM
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