Solved

Can MKDIR in Access VBA automatically create intermediate folders?

Posted on 2015-01-27
5
214 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

822 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