Creating Folders help

J

JayM

I have written the below code. The code creates a folder dependent on the
information from a form.

That bit works fine but I can't get it to check whether the folder already
exists and if so prompt the user to change the name. I have tried it with an
inputbox, message box and with another form.

Any help would be greatly appreciated.

Private Sub cmdOK_Click()

Dim MyFilePath As String
Dim TestFolder
Dim TestAlphaFolder

If cboDept.Value = "Stourport" Then MyFilePath = "w:\data\_Clients\"
If cboDept.Value = "Worcester" Then MyFilePath = "w:\data\_Clients\"
If cboDept.Value = "Kidderminster - Business" Then _
MyFilePath = "w:\data\Business\_Clients\"
If cboDept.Value = "Kidderminster - Civil" Then _
MyFilePath = "w:\data\Business\_Clients\"
If cboDept.Value = "Kidderminster - Crime" Then _
MyFilePath = "w:\data\Business\_Clients\"
If cboDept.Value = "Kidderminster - Family" Then _
MyFilePath = "w:\data\Business\_Clients\"
If cboDept.Value = "Kidderminster - Probate" Then _
MyFilePath = "w:\data\Business\_Clients\"
If cboDept.Value = "Kidderminster - Property" Then _
MyFilePath = "w:\data\Business\_Clients\"

TestFolder = Dir(MyFilePath & txtMyFolderName) & "\"

'If TestAlphaFolder = Dir(MyFilePath & Left(txtMyFolderName, 1) & "\") Then
GoTo ClientFolders

If Len(TestFolder) = 0 Then
Handler:
MkDir MyFilePath & Left(txtMyFolderName, 1) & "\"
'ClientFolders:
MkDir MyFilePath & Left(txtMyFolderName, 1) & "\" & txtMyFolderName
MkDir MyFilePath & Left(txtMyFolderName, 1) & "\" & txtMyFolderName &
"\Bills"
MkDir MyFilePath & Left(txtMyFolderName, 1) & "\" & txtMyFolderName &
"\Documents"
MkDir MyFilePath & Left(txtMyFolderName, 1) & "\" & txtMyFolderName &
"\Correspondence"
Else
txtMyFolderName = MsgBox("File " & txtMyFolderName & " already exists" _
& Chr(10) & Chr(10) & "Please type another folder name", vbOKOnly,
"File Exists")
End If

frmFolderCreation.Hide

End Sub
 
K

Kevin B

You can use the following formula, which returns a True/False result, to
determine if a folder exists or not:

Function DirExists(strPath As String) As Boolean

DirExists = (Len(Dir(strPath, vbDirectory)) > 0)

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top