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.
deer777Asked:
Who is Participating?
 
NorieVBA ExpertCommented:
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

0
 
PatHartmanCommented:
If you happened to look at the question list, you would have seen my answer to the question immediately prior to yours.

https://www.experts-exchange.com/questions/29065725/How-create-a-folder-on-a-server-with-the-PK-as-the-folder-name.html
0
 
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:

CreatePath(Me!YourTextbox.Value)

Open in new window

/gustav
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
deer777Author Commented:
Nori

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.
0
 
deer777Author Commented:
Nori

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.
0
 
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.