Avatar of filtrationproducts
filtrationproducts
Flag 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

Visual Basic ClassicMicrosoft Access

Avatar of undefined
Last Comment
VBRocks

8/22/2022 - Mon
SOLUTION
Jason Evans

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
filtrationproducts

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

  MkDir FolderName
ASKER CERTIFIED SOLUTION
VBRocks

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
filtrationproducts

ASKER
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

VBRocks

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
filtrationproducts

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

Thanks!
filtrationproducts

ASKER
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"
VBRocks


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\""

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
filtrationproducts

ASKER
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
VBRocks

So create each one separately:

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

filtrationproducts

ASKER
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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
VBRocks


Cool!  Great job!