Solved

Can MKDIR in Access VBA automatically create intermediate folders?

Posted on 2015-01-27
5
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 85
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

696 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