Solved

Can MKDIR in Access VBA automatically create intermediate folders?

Posted on 2015-01-27
5
227 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 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

733 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