Go Back   Access World Forums > Microsoft Access Reference > Code Repository

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 09-24-2015, 01:33 PM   #1
Rx_
Nothing In Moderation
 
Rx_'s Avatar
 
Join Date: Oct 2009
Location: Denver, Colorado
Posts: 2,794
Thanks: 635
Thanked 336 Times in 307 Posts
Rx_ has a spectacular aura about Rx_ has a spectacular aura about Rx_ has a spectacular aura about
Lightbulb MkDir - Make Directory from string with several layers of folders

A subroutine that can create multiple level folders in one singe call.

mkDir call results with a Runtime error 76 "Path not found"
The MkDir can only create one additional folder to an existing directory.
e.g. C:\MyGoodness using MkDir it will fail trying to create C:\MyGoodness\YourGoodness\TheirGoodness

In this example, all files go to an X:\Reg\Database Reports folder
But, each user's report goes into a folder with that users's LanID plus into a folder with the specific Report Name. (a.k.a. Automatic filing system).
Each Report has the Report name + dateTime not shown here.

Problem: The first time a new user runs a report - the MkDir must make 2 folders at once.

To save time and space, the variable are not declared here.
Code:
ReportPathName ="Minority Report"   ' its an old movie and a new TV series
UserLogin = Environ("username") ' or use your favorite method
UserPath = ("X:\Reg\Database Reports\" & UserLogin & "\" & ReportPathName & "\")
strNewReportPath = UserPath ' redundant here but used in other code not shown
' Is there already a folder or does a new folder need to be created for the new user or the first time a report name is run.
DirName = strNewReportPath
    If Dir(DirName, vbDirectory) = "" Then
    If MsgBox("Is it OK to create a new folder in X:\Reg\Database Reports\" & UserLogin & "\" & ReportPathName & "? (recommended yes)", vbOKCancel) = vbOK Then
' If this was a first time user 2 directories  needs to be created for both the user and the report
  DirName = UserPath
  MakeDirectory DirName  'MkDir DirName will only add one \ at a time so use this public subroutine
  Err.Clear
Else
    MsgBox "Create new folder cancelled. Folder not created.", vbOKOnly, "Report Cancelled, must allow folder to be created - (feeling of total rejection)"
  Exit Function
End If
' ..... Continue with the report code ...
Here is the public Subroutine to create multiple folders with MkDir
Code:
Public Sub MakeDirectory(FolderPath As String)
On Error GoTo errTrap
Dim x, i As Integer
Dim strPath As String
x = Split(FolderPath, "\")

For i = 0 To UBound(x) - 1
    strPath = strPath & x(i) & "\"
    If Not FolderExists(strPath) Then MkDir strPath
Next i
Exit Sub
errTrap:
MsgBox "There was an error trying to make a New Directory", vbOKOnly +vbCritical, "Error In Process"
err.Raise 656 ' Use this custom error to raise to the caller for identification

End Sub

__________________
Were you lucky enough to get an answer? Please mark your question as [SOLVED] The original poster can go to Thread Tools to mark it as Solved.

Quotation Thomas Jefferson: "Peace is that brief glorious moment in history when everybody stands around reloading."

There are 2 Kinds of Countries on this Planet
1. Those that use the Metric System
2. Those that had a man walk on the moon

Denver, Colorado - The "Mile High City" - non-metric!
Rx_ is offline   Reply With Quote
The Following User Says Thank You to Rx_ For This Useful Post:
hassanogaibi (06-28-2016)
Reply

Tags
error 76 , mkdir , multiple folders

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Make folders from query BBBryan Modules & VBA 13 02-24-2015 11:20 AM
Count the number of text files in directory sub folders JohnLee Modules & VBA 1 03-14-2014 03:23 AM
Write names of folders to table once added to directory? chris-uk-lad Modules & VBA 14 11-27-2008 06:11 AM
Creating Directory Folders Based On Query Results rustyCrVx Modules & VBA 6 10-17-2008 03:41 PM
Directory Search for String lcannon Modules & VBA 1 09-09-2005 11:37 AM




All times are GMT -8. The time now is 09:42 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World