VBA Code for Access to create and name a folder if it does not exist

I need VBA Code for Access to first check and see if a folder exists and if it does not then create and name it based on the number shown on a textbox on the main form.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Where does the folder name you want to check for come from?

Is it the textbox on the main form?

This code will take the value from TextBox1 and check if there is a folder with that name in 'C:\'.

If the folder doesn't exist it will be created.
Dim strFolder As String
Dim strRoot As String

    strRoot = "C:\"

    strFolder = Me!TextBox1.Value ' [Forms]![MainForm]!TextBox1.Value

    If Len(Dir(strRoot & strFolder, vbDirectory)) = 0 Then
        MkDir strRoot & strFolder
    End If

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If you happened to look at the question list, you would have seen my answer to the question immediately prior to yours.

Gustav BrockCIOCommented:
There is a simple and direct call for this.
Copy/paste into a new module:

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

Public Sub CreatePath(ByVal Path As String)
    If Right(Path, 1) <> "\" Then
        Path = Path & "\"
    End If
    MakeSureDirectoryPathExists Path
End Sub

Open in new window

Then call:


Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

deer777Author Commented:

I have input this code but [unothing happens[/u] when I press the button that I put the code on.

The textbox is on the main form.
deer777Author Commented:

I was wrong, it did create the folder as requested.  

What I need the code to do now is if it finds it initially, I need it to open it.  

If it doesn't find it, I need it to create it and then open it.  

What can I add to the code to get it to open the folder if it exists or after it creates it.
Gustav BrockCIOCommented:
You can use the function above. First call:

CreatePath Me!YourTextbox.Value

Open in new window

to create the path should it not exist.
Then use FileDialog to open the dialog window, like:

Private Sub txtTarget_DblClick(Cancel As Integer)

    Dim Dialog      As FileDialog
    Dim Selected    As Long
    Set Dialog = FileDialog(msoFileDialogSaveAs)
    With Dialog
        .AllowMultiSelect = False
        .InitialFileName = Nz(Me!txtTarget.Value)
        .Title = "Name saved file"
        Selected = .Show
        If Selected <> 0 Then
            Me!txtTarget.Value = .SelectedItems.Item(1)
        End If
    End With

End Sub

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.