Solved

Can MKDIR in Access VBA automatically create intermediate folders?

Posted on 2015-01-27
5
202 Views
Last Modified: 2015-01-28
I have code which creates a folder for a new invoice but the folders are in sub-folders by year. For instance, invoice 12345 would go into folder:

c:\invoices\2015\12345

Right now, the folder c:\invoices\2015 exists but not c:\invoices\2016

Is there a way to have MKDIR create any missing intermediate folders so that in 2016, c:\invoices\2016 will automatically be created when I try to create c:\invoices\2016\[invoice#] (similar to how the DOS command works if Command Extensions are enabled)? Or do I need to create the intermediate folders explicitly?
0
Comment
Question by:jrmcanada2
5 Comments
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 167 total points
ID: 40573357
The filesystemobject is your friend.
Check it out.
It is very much the preferred way of working with files and folders.
Dir() and Kill have their place, too, but for what you asked:

Dim fs As Object
Dim BuiltPath As String
BuiltPath = "c:\invoices\2016\SomeInvoiceNumber" 
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(BuiltPath) = False Then
    fs.CreateFolder BuiltPath
End If
set fs = nothing

Open in new window

is definitely the way to go.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 167 total points
ID: 40573375
place this code in a regular module


Public Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long

to use

MakeSureDirectoryPathExists  "c:\invoices\2016\folder1\folder2\"

and it will create all the folders if don't exists
0
 
LVL 84
ID: 40574182
It is very much the preferred way of working with files and folders.
In many environments you cannot use the Scripting Runtime, which is what drives the FileSystemObject classes. In some cases it's just a matter of adding exceptions to the virus engine, but I've run into situations where the IT department would not do that, and I've had to develop other methods (like the ones rey describes).
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40574215
It's one thing to block .vbs files and wscript.exe and cscript.exe
But to blow up only some of the stuff called from a trusted VBA document?!?
But then, there's no accounting for the insanity of Symantec and McAfee.
They had loveletter and Melissa on the brain for quite a while.

So, if it is permitted, the filesystemobject is your friend.
If it is not permitted then you'll have to do the exact same thing with a different friend.

:)
Nick67
0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 166 total points
ID: 40574475
Since the DOS mkdir will create the path, just shell out the mkdir command, rather than use the VB mkdir command.
shell "cmd /c mkdir ""c:\invoices\2016\12345"""

Open in new window


You might also instantiate a wscript.shell object and use its Run method as a Shell alternative if you are permitted to use that ActiveX object.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now