Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MkDir for more than one sub folder

Status
Not open for further replies.

Clarinet

Technical User
Mar 21, 2005
20
US
The following code from PHV a few years ago is great for making a new directory when more than one sub folder may need to be created.
Code:
Sub myMkDir(strFolderName As String)
On Error Resume Next
Dim a, t As String, i As Integer
[b][COLOR=red]a = Split(strFolderName, "\")[/color][/b]
t = a(0)
For i = 1 To UBound(a)
t = t & "\" & a(i)
MkDir t
Next
End Sub

However, the code window pops up (when no break point was set in the code) with the red line highlighted in yellow (like it would if a break point had been set by a developer).

I used his code as a function in a module and the function is called from the following:
Code:
myPath = Application.CurrentProject.Path & "\" & CurrYear & "\" & "Qtr" & CurrQtr

If Len(Dir(myPath, vbDirectory)) = 0 Then
    myMkDir (myPath)
End If

This only happens the first time the new directory is created and a user would have no idea how to proceed! Any insight as to why this happens would be greatly appreciated.
 
Instead of on error resume next, change that to On Error Goto Err_h:

Sub myMkDir(strFolderName As String)
On Error goto Err_h
Dim a, t As String, i As Integer
a = Split(strFolderName, "\")
t = a(0)
For i = 1 To UBound(a)
t = t & "\" & a(i)
MkDir t
Next
Exit Sub

'*****
err_h:
'*****
Msgbox Error$
Exit Sub


End Sub


That might give you more information on any error. Also, right mouse click the line and see if you get a Toggle Breakpoint option, click it and if there is a breakpoint set, it will toggle it off.



 
Also add in the lines in red or you'll error out because the Application.CurrentProject.Path does exist.


Sub myMkDir(strFolderName As String)
On Error goto Err_h
Dim a, t As String, i As Integer
a = Split(strFolderName, "\")
t = a(0)
For i = 1 To UBound(a)
t = t & "\" & a(i)
If Len(Dir(t, vbDirectory)) = 0 Then
MkDir t
Endif

Next
Exit Sub

'*****
err_h:
'*****
Msgbox Error$
Exit Sub
 
Thanks to vbajock and StarPassing for such quick solutions. Both suggestions work on my PC at home, but then the original code also worked at home!

I will be in the office next Tuesday and the office PC is where the problem occured (both are XP). I will certainly post the results of the test on Tuesday.
 
When I am trouble shooting code, I usually add

'*****
err_h:
'*****
Msgbox Error$

Stop
Resume
Exit Sub

to the error handler. That way, you get the error message, the code stops on the stop statement, and then you can use the F8 key to step to the Resume, and then hit F8 again and it will return you to the statement causing the error. You want to remove the Stop and the Resume before you give the program to your users, and replace it with an information message and follow it with code to exit with blowing everything up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top