Morning all,
I'm trying to debug this code which worked perfectly with just the [Job Number] field as the folder name, I'm now trying to add in the customer name to the path but when the directory does not exist, the make directory function fails with a path not found error:
If I manually create the folder, it will open it just fine
I've added in the msg box to validate the path and it all looks OK?
I'm trying to debug this code which worked perfectly with just the [Job Number] field as the folder name, I'm now trying to add in the customer name to the path but when the directory does not exist, the make directory function fails with a path not found error:
If I manually create the folder, it will open it just fine
I've added in the msg box to validate the path and it all looks OK?
Code:
Private Sub Command616_Click()
If IsNull([Job Number].Value) Then
MsgBox "Client Job Number cannot be blank", vbOKOnly
Exit Sub
Else
Dim stAppName As String
Dim CreateFolder As Integer
Dim jFolder1 As String
Dim jFolder2 As String
Dim jFolder As String
Dim ForbiddenChars As Variant
Dim i As Integer
'define array of forbidden characters to replace
ForbiddenChars = Array("/", "\", ":", "*", "?", """", "<", ">", "|")
'clean up Customer Name and Job Number
jFolder1 = Me![Customer Name]
jFolder2 = Me![Job Number]
'replace forbidden characters in jFolder1 and jFolder2
For i = LBound(ForbiddenChars) To UBound(ForbiddenChars)
jFolder1 = Replace(jFolder1, ForbiddenChars(i), "-")
jFolder2 = Replace(jFolder2, ForbiddenChars(i), "-")
Next i
'concatenate jFolder1 and jFolder2 to create final path
jFolder = jFolder1 & "\" & jFolder2
'check if folder exists
If Dir("L:\Client_Images\" & jFolder, vbDirectory) <> "" Then
stAppName = "C:\windows\explorer.exe L:\Client_Images\" & jFolder
Call Shell(stAppName, 1)
Else
' ask user if we are to create the folder
CreateFolder = MsgBox("Folder does not exist! Create it?", vbYesNo)
If CreateFolder = vbNo Then
Exit Sub
ElseIf CreateFolder = vbYes Then
'create the folder and open in windows explorer
MsgBox "L:\Client_Images\" & jFolder, vbOKOnly
On Error GoTo Error_Handler
MkDir "L:\Client_Images\" & jFolder & "\"
stAppName = "C:\windows\explorer.exe L:\Client_Images\" & jFolder
Call Shell(stAppName, 1)
End If
End If
End If
Exit Sub
Error_Handler:
MsgBox "Error creating folder: " & Err.Description, vbCritical, "Folder Creation Error"
Exit Sub
End Sub