I have a form that contains two text boxes where the user can type in file paths, I want to check to see if the file path entered into the textboxes are valid when the user clicks on the accept button. Here is my code

Private Sub btnAccept_Click()

Dim OpenFile As Integer

OpenFile = FreeFile

'Create and write to .ini file
Open App.Path & "\HistoricalPlayback.ini" For Output As OpenFile
    Print #OpenFile, "File Configuration: " & txtFFP
    Print #OpenFile, "Database Configuration: " & txtDBFP
Close OpenFile

If Dir$(txtFFP.Text) <> "" Or Dir$(txtDBFP.Text) <> "" Then
    Exit Sub
Else
    MsgBox "Enter Valid File Path"
End If
   
glbFilePath = txtFFP.Text
glbDBPath = txtDBFP.Text
End Sub

add this function :

Function FileExists(FileName As String) As Boolean
    On Error GoTo ErrorHandler
    ' get the attributes and ensure that it isn't a directory
    FileExists = (GetAttr(FileName) And vbDirectory) = 0
ErrorHandler:
    ' if an error occurs, this function returns False
End Function
Private Sub btnAccept_Click()

Dim OpenFile As Integer

OpenFile = FreeFile

'Create and write to .ini file
Open App.Path & "\HistoricalPlayback.ini" For Output As OpenFile
    Print #OpenFile, "File Configuration: " & txtFFP
    Print #OpenFile, "Database Configuration: " & txtDBFP
Close OpenFile

[B]If FileExists(txtFFP.text) = True And FileExists(txtDBFP.Text) = True Then
    MsgBox "File Path is Valid"[/B]
Else
    MsgBox "Enter Valid File Path"
End If
   
glbFilePath = txtFFP.Text
glbDBPath = txtDBFP.Text
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.