Creating Folders from a Form

J

JayM

Hi
I have a form (frmFolderCreation) with a combobox (cboDept) and a text field
(txtMyFolderName).

The idea of this is to create folders automatically dependent upon which
department users are in and once it creates the folder will then copy some
standard folders into it.

My problem is, having written the code, with lots of help from the internet
because I am a novice at this stuff, if the folder already exists it gives me
a message to say so but then closes the form - how can I stop the form
closing in this instance so that the user can type another name but close if
the folder is just created.

My code is :

Private Sub cmdOK_Click()
Dim MyFilePath As String
Dim strMsg As String

Select Case cboDept.Value
'Stourport and Worcester have the same path
'so if cboDept.Value is either one, we treat
'with the same Case conditions
Case "Stourport", "Worcester"
MyFilePath = "w:\data\_Clients\"
Case "Kidderminster - Business"
MyFilePath = "w:\data\Business\_Clients\"
Case "Kidderminster - Civil"
MyFilePath = "w:\data\Business\_Clients\"
Case "Kidderminster - Crime"
MyFilePath = "w:\data\Crime\_Clients\"
Case "Kidderminster - Family"
MyFilePath = "w:\data\Family\_Clients\"
Case "Kidderminster - Probate"
MyFilePath = "w:\data\Probate\_Clients\"
Case "Kidderminster - Property"
MyFilePath = "w:\data\Property\_Clients\"
Case Else
strMsg = "Unexpected Selection in cboDept."
End Select

Dim fso
Dim fol As String
Dim sfol As String


fol = MyFilePath & Left(txtMyFolderName, 1) & "\" & txtMyFolderName
sfol = MyFilePath & "\_DUMMY FOLDER"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
Else
MsgBox fol & " already exists!", vbExclamation, "Folder Exists"
End If
Unload frmFolderCreation
On Error Resume Next
If Not fso.FolderExists(sfol) Then
MsgBox sfol & " is not a valid folder/path.", vbExclamation, "Invalid
Source"
ElseIf Not fso.FolderExists(fol) Then
MsgBox fol & " is not a valid folder/path.", vbExclamation, "Invalid
Destination"
Else
fso.CopyFolder sfol, fol
End If
If Err.Number = 53 Then MsgBox "File Not Found"
End Sub

Any help with this would be very much appreciated.

JayM
 
D

David Sisson

You need to create a errorhandler section. See http://tinyurl.com/294e67
for another example.

.... other code here

On Error Goto Errorhandler
fol = MyFilePath & Left(txtMyFolderName, 1) & "\" & txtMyFolderName
sfol = MyFilePath & "\_DUMMY FOLDER"
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder (fol)
Else
MsgBox fol & " already exists!", vbExclamation, "Folder Exists"
End If
Unload frmFolderCreation
On Error Resume Next
If Not fso.FolderExists(sfol) Then
MsgBox sfol & " is not a valid folder/path.", vbExclamation, "Invalid
Source"
ElseIf Not fso.FolderExists(fol) Then
MsgBox fol & " is not a valid folder/path.", vbExclamation, "Invalid
Destination"
Else
fso.CopyFolder sfol, fol
End If

Exit Sub

Errorhandler:
If Err.Number = 53 Then MsgBox "File Not Found"
End Sub

I like using Select Case instead of If-Then. Just a personal
preference.

David
 
H

Helmut Weber

Some, not all, in a nutshell:

Sub Macro3()
If Dir("c:\test", vbDirectory) = "" Then
MkDir "c:\test\"
Else
MsgBox "We had that already"
End If
End Sub


HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

JayM

Having thought about it and had it confirmed and tested it all I needed to do
was to move my unloadform command into my if statement!!

JayM
 

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