Custom function to create folder using VBA

Create Folder using VBA

This article is about to create a folder using Visual basic editor. We often need to create folder on local machine and remote machine. In this article we create folder using customized function. Using built in function some time mkDir function not work for creating folder inside folder. Using this custom function we are able to create mkdir function which would create child folder inside parent one.

For implementation of this firstly we are creating a form and button as shown in Fig 1.1. Then on button click we are calling the mkDir function.

How to create the folder using VBA. Fig-1.1

Fig-1.1

After coding on button click we have to open the path that we specify. You will found there is new folder is created as shown in Fig 1.2.

How to create the folder using VBA. Fig-1.2

Fig-1.2

VBA Code

Option Compare Database
Private Sub Command0_Click()
MakeDir ("D:\Custom_Folder\New_Folder")
End Sub

Public Function MakeDir(ByVal STRPATH As String) As Boolean
If Right(STRPATH, 1) = "\" Then
STRPATH = Left(STRPATH, Len(STRPATH) - 1)
End If
Dim SPLITSTRPATH() As String
SPLITSTRPATH = Split(STRPATH, "\")
Dim VAR1 As Integer
Dim MERGE As String
For VAR1 = 0 To UBound(SPLITSTRPATH)
If VAR1 <> 0 Then
MERGE = MERGE & "\"
End If
MERGE = MERGE & SPLITSTRPATH(VAR1)
If Dir(MERGE, vbDirectory) = "" Then
MkDir MERGE
End If
Next
MakeDir = True
Exit Function
End Function


DISCLAIMER

It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. Access Guru will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.