Link to home
Start Free TrialLog in
Avatar of deer777
deer777Flag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
Avatar of deer777

ASKER

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.
Avatar of deer777

ASKER

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