Solved File Dialog window

E9-Tech

Member
Local time
Today, 23:04
Joined
Apr 28, 2021
Messages
36
I have a code to add a file link to a txt box and another code to access this file, is all working perfectly and have a minor issue if I decide not to add the link and pres Cancel within the dialog window

the errror is
1737905588557.png


The code is:
Code:
Private Sub CmdAddPayslip_Click()
    With Application.FileDialog(msoFileDialogFilePicker)
    .Show
    Me!PayslipLink = .SelectedItems(1)

End With
    
End Sub

How do I handle this error within the code?

Also is there a way to open the dialog box within a specific folder rather than documents?

Thanks
 
You'll need to add a conditional to check whether the user pressed the Cancel button in the File Dialog. That would go
after the line:

.Show

and before you try to assign the selected item to the value.

Someone probably knows off the top of their head the value returned by the Cancel button in the File Dialog, but you can determine that yourself by testing it.

======

Coincidentally, I just answered a question on another forum on a highly related topic.

This procedure relies on a path stored in a lookup table. It's retrieved by the line:

payrollfiles = pp.PayrollPathIn

You could specify your path by just hard-coding or using a lookup to a stored path in a table.


Code:
'---------------------------------------------------------------------------------------
' Procedure : AddPayrollFile
' Author    : George
' Date      : 7/15/2010
' Purpose  : Old code recycled here to look for three types of files in a designated folder
'---------------------------------------------------------------------------------------
'
Private Function AddPayrollFile(varItems As Variant, intStart As Integer) As Long
    Dim MyPath As String
    Dim fs
    Dim intPos As Integer
    Dim payrollfiles As String
    Dim pp As New ProjectPath
On Error GoTo errhandler

    payrollfiles = pp.PayrollPathIn
    intPos = intStart
    MyPath = payrollfiles  ' Set the path.
    ChDir payrollfiles

    Set fs = Application.FileSearch
          With fs
                .NewSearch
                .lookin = MyPath
                .SearchSubFolders = False
                .filetype = 1
                .FileName = "*.GHN;*.GLI;*.TXT" ' Syntax is semi-colon separated wildcard extensions with no spaces
             
              If .Execute > 0 Then
                    For intPos = intStart To .FoundFiles.Count - 1
                    varItems(intPos) = .FoundFiles(intPos + 1)
                    Next intPos
                End If
          End With
AddPayrollFile = intPos

exitProc:
Exit Function
errhandler:
    If Err <> 2455 Then
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure AddPayrollFile of Module modImportFileDirectory"
    End If
    Resume exitProc
    Resume
   
End Function
 
The .Show method actually returns a boolean value.

True if the user clicked OK (or whatever the caption) and False if they cancelled.

So adjust your code:
Code:
Private Sub CmdAddPayslip_Click()
  With Application.FileDialog(msoFileDialogFilePicker)
    If .Show Then
      Me!PayslipLink = .SelectedItems(1)
    Else
      ' user cancelled
    End If
  End With 
End Sub
 
As for folder.
 
The .Show method actually returns a boolean value.

True if the user clicked OK (or whatever the caption) and False if they cancelled.

So adjust your code:
Code:
Private Sub CmdAddPayslip_Click()
  With Application.FileDialog(msoFileDialogFilePicker)
    If .Show Then
      Me!PayslipLink = .SelectedItems(1)
    Else
      ' user cancelled
    End If
  End With
End Sub
Thanks this did the trick!
 

Users who are viewing this thread

Back
Top Bottom