Link to home
Start Free TrialLog in
Avatar of filtrationproducts
filtrationproductsFlag for United States of America

asked on

Create Folder in Visual Basic

I currently use the code inside a visual basic application to create the folder in the format below;

V:\7.3\F730000_Hyperlinks\PMR-1_Documentation\

I would like it to create the Documentation folder inside the PMR-1 instead like this;

V:\7.3\F730000_Hyperlinks\PMR-1\Documentation\

Is this possible? I attached the code I use now below.
Private Sub Command99_Click()
        Dim strPath As String
    Dim strFolderName As String
    Dim FolderName As String
 
strPath = "V:\7.3\F730000_Hyperlinks\"
strFolderName = "PMR-" & Me.ID_PMR.Value & "_CustomerCommunication"
FolderName = (strPath & strFolderName)
 
If Len(Dir(FolderName, vbDirectory)) = 0 Then
    MkDir FolderName
    Call Shell("explorer.exe " & FolderName, 1)
Else
    Call Shell("explorer.exe " & FolderName, 1)
End If
    
End Sub

Open in new window

SOLUTION
Avatar of Jason Evans
Jason Evans
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of filtrationproducts

ASKER

I tried that and when the code runs I get an error on this line;

  MkDir FolderName
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
VBRocks,
That also gives me an error on the MkDir FolderName line
Private Sub Command98_Click()
        Dim strPath As String
    Dim strFolderName As String
    Dim FolderName As String
 
strPath = "V:\7.3\F730000_Hyperlinks\"
strFolderName = "PMR-" & Me.ID_PMR.Value & "\Documentation\"
FolderName = (strPath & strFolderName)
 
If Len(Dir(FolderName, vbDirectory)) = 0 Then
    MkDir FolderName
    Call Shell("explorer.exe " & FolderName, 1)
Else
    Call Shell("explorer.exe " & FolderName, 1)
End If
    
End Sub

Open in new window

What kind of error are you getting?  It worked perfect for me.

Very Strange... I tried it again after closing Access and reopening Access and it started working properly.

Thanks!
Now its not working again and I havent changed anything. All I did was close and reopen it and it will not create the folder. If the folder exists already it will open but if it doesnt exist it will error on the MkDir command saying "path not found"

Do all of the parent folders exist?

For example,

"V:\7.3" must exist before you can try to create "V:\7.3\F730000_Hyperlinks\"

And...  "V:\7.3\F730000_Hyperlinks\PMR-1" must exist before you try to create
"V:\7.3\F730000_Hyperlinks\PMR-1\Documentation\""

The only folder that exists is "V:\7.3\F730000_Hyperlinks\"
Everything after that needs to be created by the code... \PMR-#\DOCUMENTATION\

So with that code I am using now it is creating two folders \PMR-1 and \Documentation\ within that folder
So create each one separately:

MkDir "V:\7.3\F730000_Hyperlinks\PMR-1"
MkDir "V:\7.3\F730000_Hyperlinks\PMR-1\Documentation"

I got it to work.
    Dim strPath As String
    Dim strPath2 As String
    Dim strFolderName As String
    Dim FolderName As String
    Dim FolderName2 As String
 
strPath = "V:\7.3\F730000_Hyperlinks\"
strFolderName = "PMR-" & Me.ID_PMR.Value
FolderName = (strPath & strFolderName)
 
strPath2 = "V:\7.3\F730000_Hyperlinks\" & strFolderName & "\Documentation"
strFolderName2 = "\Documentation\"
FolderName2 = (FolderName & strFolderName2)
If Len(Dir(FolderName, vbDirectory)) = 0 Then
    MkDir FolderName
End If
 
If Len(Dir(FolderName2, vbDirectory)) = 0 Then
    MkDir FolderName2
    Call Shell("explorer.exe " & FolderName2, 1)
Else
    Call Shell("explorer.exe " & FolderName2, 1)
End If

Open in new window


Cool!  Great job!