Skip to content

Instantly share code, notes, and snippets.

@WyattCast44
Last active January 6, 2021 00:05
Show Gist options
  • Save WyattCast44/5156eb0316cb4651f7bbf64e06741b10 to your computer and use it in GitHub Desktop.
Save WyattCast44/5156eb0316cb4651f7bbf64e06741b10 to your computer and use it in GitHub Desktop.
A collection of VBA for MS Access functions related to dealing with the filesystem
Option Compare Database
Option Explicit
''
'' Function: createFolder
'' ===========================================
''
'' Params
'' ===========================================
'' - path (string|required): The path to create the folder
'' - failIfAlreadyExists (boolean|optional|default=False): If the folder already exists should the function indicate failure
'' - successCallback (string|optional|default=Null): The function to call on success, must conform to standard success callback interface
'' - errorCallback (string|optional|default=Null): The function to call on error, must conform to standard success callback interface
''
'' Return
'' ===========================================
'' - boolean
''
'' Author
'' ===========================================
'' - Wyatt Castaneda https://github.com/wyattCast44/
''
'' References
'' ===========================================
'' - https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/createfolder-method
''
Public Function createFolder(path As String, Optional failIfAlreadyExists As Boolean = False, Optional successCallback As Variant = Null, Optional errorCallback As Variant = Null) As Boolean
On Error GoTo handleError
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If path <> "" Then
FSO.createFolder path
End If
If FSO.folderExists(path) Then
GoTo handleSuccess
Else
GoTo handleError
End If
Exit Function
handleSuccess:
createFolder = True
If Not IsNull(successCallback) Then
Application.Run successCallback, path
End If
GoTo cleanUp
Exit Function
handleError:
If Err.Number = 58 And Not failIfAlreadyExists Then
createFolder = True
GoTo handleSuccess
Else
createFolder = False
If Not IsNull(errorCallback) Then
Application.Run errorCallback, Err.Number, Err.Description, "createFolder", path
End If
End If
GoTo cleanUp
cleanUp:
Set FSO = Nothing
Exit Function
End Function
Option Compare Database
Option Explicit
''
'' Function: deleteFile
'' ===========================================
''
'' Params
'' ===========================================
'' - path (string|required): The path to the file to delete
'' - failIfAlreadyExists (boolean|optional|default=False): If the file does not exists, should the function indicate failure
'' - successCallback (string|optional|default=Null): The function to call on success, must conform to standard success callback interface. successCallback(functionName, path)
'' - errorCallback (string|optional|default=Null): The function to call on error, must conform to standard success callback interface
''
'' Return
'' ===========================================
'' - boolean
''
'' Author
'' ===========================================
'' - Wyatt Castaneda https://github.com/wyattCast44
''
'' References
'' ===========================================
'' - https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/deletefile-method
''
Public Function deleteFile(path As String, Optional failIfFileDoesntExist As Boolean = False, Optional successCallback As Variant = Null, Optional errorCallback As Variant = Null) As Boolean
On Error GoTo handleError
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.fileExists(path) Then
FSO.deleteFile path
GoTo handleSuccess
Else
If failIfFileDoesntExist Then
GoTo handleError
Else
GoTo handleSuccess
End If
End If
Exit Function
handleSuccess:
deleteFile = True
If Not IsNull(successCallback) Then
Application.Run successCallback, path
End If
GoTo cleanUp
Exit Function
handleError:
deleteFile = False
If Not IsNull(errorCallback) Then
Application.Run errorCallback, Err.Number, Err.Description, "deleteFile", path
End If
GoTo cleanUp
Exit Function
cleanUp:
Set FSO = Nothing
Exit Function
End Function
Option Compare Database
Option Explicit
''
'' Function: deleteFolder
'' ===========================================
''
'' Params
'' ===========================================
'' - path (string|required): The path to the folder to delete
'' - failIfDoesNotExist (boolean|optional|default=False): If the folder does not exist, should the function indicate failure
'' - successCallback (string|optional|default=Null): The function to call on success, must conform to standard success callback interface
'' - errorCallback (string|optional|default=Null): The function to call on error, must conform to standard success callback interface
''
'' Return
'' ===========================================
'' - boolean
''
'' Author
'' ===========================================
'' - Wyatt Castaneda https://github.com/wyattCast44
''
'' References
'' ===========================================
'' - https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/deletefolder-method
''
Public Function deleteFolder(path As String, Optional failIfDoesNotExist As Boolean = False, Optional successCallback As Variant = Null, Optional errorCallback As Variant = Null) As Boolean
On Error GoTo handleError
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
If path <> "" Then
GoTo handleError
End If
path = IIf(Right(path, 1) = "\", Left(path, Len((path)) - 1), path)
If FSO.folderExists(path) Then
FSO.deleteFolder path
GoTo handleSuccess
Else
If failIfDoesNotExist Then
GoTo handleError
End If
End If
Exit Function
handleSuccess:
deleteFolder = True
If Not IsNull(successCallback) Then
Application.Run successCallback, path
End If
GoTo cleanUp
Exit Function
handleError:
deleteFolder = False
If Not IsNull(errorCallback) Then
Application.Run errorCallback, Err.Number, Err.Description, "deleteFolder", path
End If
GoTo cleanUp
cleanUp:
Set FSO = Nothing
Exit Function
End Function
Public Function handleError(errorNum As Variant, errDesc As Variant, Optional functionName As String = "", Optional params As Variant)
'
' Handle your errors here
'
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment