small image for MakeAPath Ms Access Gurus

VBA > File > Make A Path

If path doesn't exist, then make each folder it needs. Return True if path is good.

Screen shot

One or several folders can be created — whatever is needed.

image for MakeAPath

Examples

Logic

Set up error handler and initialize the return value to be False.

If the directory is already there, return True and exit.

If the path passed does not end with backslash, then add it.

Starting with the first backslash (\), create what is needed if it doesn't already exist.

If the drive letter is bad, the path will not be able to be created and the function will return False.

Loop through each specified folder. If it doesn't exists, then create it using MkDir. DoEvents, which may not be necessary, ensures that Access sees the new path.

In the end, test the final path to see if is a directory using Dir and specify vbDirectory (16) for the optional second parameter.

Compile and save the code. When prompted for the module name, if you don't have your own name, use this so the module name is logical and not the same as any procedure name:

Parameters

Return

Return True if creating path was successful, or path already exists. Return False if path doesn't exist and couldn't be created.

Code

' Module Name: mod_Dir_MakeAPath
'*************** Code Start *****************************************************
' Purpose  : Make a path if necessary
' Author   : crystal (strive4peace)
' Return   : boolean
' License  : below code
' Code List: www.MsAccessGurus.com/code.htm
'--------------------------------------------------------------------------------

' MakeAPath

'--------------------------------------------------------------------------------' Public Function MakeAPath( _ psPath As String ) As Boolean 'crystal (strive4peace) ...190204 'set up error handler On Error GoTo Proc_Err 'initialize return value to be False for not successful MakeAPath = False 'if directory is already there, return True and exit If Len(Dir(psPath, vbDirectory)) > 0 Then MakeAPath = True GoTo Proc_Exit End If 'dimension variables Dim i As Integer _ , iPos As Integer _ , sPath As String 'add backslash to end of path if necessary iPos = 1 If Right(psPath, 1) <> "\" Then psPath = psPath & "\" 'get position of first backslash iPos = InStr(iPos, psPath, "\") 'loop through directories of path and make folders Do While iPos > 0 sPath = Left(psPath, iPos) If Len(Dir(sPath, vbDirectory)) = 0 Then MkDir sPath DoEvents End If 'set start search position to be 1 + position of last backslash found iPos = InStr(iPos + 1, psPath, "\") Loop 'if folder exists, then return True and exit If Len(Dir(psPath, vbDirectory)) > 0 Then MakeAPath = True GoTo Proc_Exit End If 'exit code Proc_Exit: On Error Resume Next Exit Function 'if there is an error, then resume with exit code Proc_Err: Resume Proc_Exit End Function ' ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** Code End *******************************************************

Goto Top  

Back Story

I use this to create folders if a path doesn't exist. Am posting it now because I intend to enhance my code to shell to Irfanview to resize images.

References

Docs / Office VBA Reference / Language reference / Reference / Functions / Dir

Help: Dir function

Docs / Office VBA Reference / Language reference / Reference / Statements / MkDir

Help: MkDir statement

Docs / Office VBA Reference / Language reference / Reference / Functions / Right

Help: Right function

Docs / Office VBA Reference / Language reference / Reference / Functions / Right

Help: DoEvents function

Goto Top  

Share with others

here's the link to copy:

https://msaccessgurus.com/VBA/Code/MakeAPath.htm

Do you have something to say or share?

It is interesting to hear from you. Was something not clear? Did you find a bug? Is an explanation wrong or not sufficient? Do you want the code do more (there is always more)?

Some of you write to say thanks and tell me what you're doing with Access ... its nice to get an echo back. I want you and others to be good with Access, and other Office applications like Excel, Word, and PowerPoint ... and Windows. Take advantage of the strengths in each to manage your information wisely.

Are you a developer? Do you want to share? Email to ask about getting your pages added to the code index.

When we communicate, collaborate, and appreciate, we all get better. Thank you. Email me at info@msAccessGurus.com

Goto Top