MkDir makes Frustration

NauticalGent

Ignore List Poster Boy
Local time
Today, 14:18
Joined
Apr 27, 2015
Messages
7,048
This morning I attempted to place a command button on one of my forms that would allow users to:

1. Make a directory on a shared drive, the path would be determined by values of the forms fields
2. Store that path in a hyper-link field so that the user could click it and be whisked away to that directory and view its contents.

Sounded easy enough and a quick Google search took me to several sites that spoke to the MkDir Function. Easy day...

Until I actually clicked the button...I kept getting an error 76 - Path does not found. Well, no s**t Sherlock, what do you think in am trying to do here?!?

Could not figure out what was wrong and was getting ready to scrap the whole idea, which was not good because I had already bragged to the users that they would be able to have it...

Then I remembered seeing something about an "overload" procedure on PBaldy's web site. It was while I was reading the notes that I came to understand that MkDir can only make one directory at a time which is not ideal especially if you are storing files on a shared drive.

This Function allows you to bypass that and more. Here is the link if anyone is interested.

http://www.baldyweb.com/ChrisOSamples.htm

The code is by Chris-O, I would thank him directly but I have heard a rumor he is no longer with us?

At any rate, it worked like a champ. If you are needing something similar, here is your answer.
 
Paste this into a module, then use it as:

makeDir "c:\folder1\folder2"

Code:
Public Sub MakeDir(ByVal pvDir)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(pvDir) Then FSO.CreateFolder pvDir     'MkDir pvDir

Set FSO = Nothing
End Sub
 
Guess I should have asked first...that is a LOT less code!
 
Interesting, I was looking for creating directory on button yesterday too. Didn't see this post at that time, but found and used below from another thread. So far is working fine. Is RanMan256 suggestion better or another way to accomplish same result?

'https://access-programmers.co.uk/forums/showthread.php?t=27374

Code:
        stDirectoryPath = "Q:\SELF-STUDY\20" & Mid(Me.cboAcro.Column(2), 2, 2) & " Courses\" & Me.cboAcro.Column(1)
        If Dir(stDirectoryPath, vbDirectory) = "" Then MkDir stDirectoryPath
 
Ranmans and the one I posted will allow you to make as many sub-directories as you wish. The code I used has a LOT of bells and whistles that are probably never going to be needed, but I like it anyway.

Ranmans is much more streamlined and is probably all you will need...
 
Paste this into a module, then use it as:

makeDir "c:\folder1\folder2"

Code:
Public Sub MakeDir(ByVal pvDir)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(pvDir) Then FSO.CreateFolder pvDir     'MkDir pvDir

Set FSO = Nothing
End Sub


[ALERT] Verified in Access 2003/2007/2010/2013: This code will only create a single folder in a folder that already exists. Works just like the MkDir().
 
Last edited:
Here is pure VBA that does create all the folders in the path:

Code:
Function CreateFolder(MyPath As String)
  
'Created by Dane A. Miller aka dallr
'create recursive folders on a path eg. "c:\dane\fred\harry" 
Dim c As Integer
On Error Resume Next

If InStr(4, MyPath, "\") = 0 Then
    MkDir (MyPath)
Else
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
        For c = 4 To Len(MyPath)
            If Mid(MyPath, c, 1) = "\" Then
                MkDir Left(MyPath, c)
            End If
        Next c
End If
End Function


Usage:

Code:
CreateFolder "d:\TopLevel\Level1\Level2"
 
Last edited:
Here is a API method that does create all the folders in the path:

Code:
Private Declare Function MakeSureDirectoryPathExists _
    Lib "imagehlp.dll" (ByVal lpPath As String) As Long

Public Sub CreateDirStruct(strTree As String)
    'Create the directory structure if it doesn't already exist.
    If Right(strTree, 1) <> "\" Then strTree = strTree & "\"
    MakeSureDirectoryPathExists strTree
End Sub

Usage:

Code:
CreateDirStruct "d:\TopLevel\Level1\Level2"
 
Last edited:

Users who are viewing this thread

Back
Top Bottom