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
Solved

Can MKDIR in Access VBA automatically create intermediate folders?

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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