I'm trying to program a browse button where you can select a program on your hard drive and output the path/filename to a text box. I think I need a control to do this but not sure. Someone Please Help!!!!
Create Common Dialog control on form and name it cmnDialog. Create command button on form and name it pbBrowse. Copy following codes into codes window.
Public strOutputFileName As String 'Default output File name '------------------------
Private Sub pbBrowse_Click()
On Error GoTo Err_pbBrowse_Click
Dim strFilter As String
Dim strFileName As String
Dim strOutputDocName As String
Dim objDialog As Object
Set objDialog = Me.cmnDialog.Object
With objDialog ' Ask for new file location.
.DialogTitle = "Open File..."
.Filter = strFilter
.FileName = strOutputFileName
.CancelError = True
.FilterIndex = 1
.flags = &H4 Or &H2 Or &H800 Or &H400 'cdlOFNHideReadOnly + cdlOFNOverwritePrompt + cdlOFNPathMustExist
.ShowOpen ' If user responded, put selection as string variable's value.
If Len(.FileName) > 0 Then 'Remember current selection
strOutputFileName = .FileName
'Update textbox with full path string
me.txtLocation=strOutputFileName
End If
End With
Exit_pbBrowse_Click:
Exit Sub
Err_pbBrowse_Click:
If Err.Number <> 32755 Then 'An error No 32755 is generated
'when the user chooses the Cancel button.
MsgBox "Error No " & Err.Number & vbLf & Err.Description
End If
Resume Exit_pbBrowse_Click
End Sub
I tried to Create a Common Dialog control on my form and I get an error: The OLE isn't registered. To register reinstall. So I used REGSVR32 COMDLG16.OCX to see if it would register but gave me a load libray error. The code you provided is great and its exactly what I am looking for. If you know the cause of the OLE Error let me know.
To do that, you will need:
a) a DriveListBox (Drive1)
b) a DirListBox (Dir1)
c) a FileListBox (File1)
d) a TextBox (txtChoice)
Then:
Private Sub Drive1_Change()
On Error Resume Next
Dir1.Path = Drive1.Drive
End Sub
Private Sub Dir1_Change()
Dim sFile As String
File1.Path = Dir1.Path
sPath = Dir1.Path
End Sub
Private Sub File1_DblClick()
Dim nCounter As Integer
Dim sFile As String
For nCounter = 0 To File1.ListCount - 1
If File1.Selected(nCounter) = True Then
sFile = File1.List(nCounter)
End If
Next
Me.txtChoice.Text = sPath & "/" & sFile
End Sub
I just copied and pasted this coding from a project I did 2 years ago. I customized it on the fly for you. But I think it should work. But basically, this is all you need to get started.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.