Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Browse Button - Code not Populating File Path Text Field

Status
Not open for further replies.

RobertIngles

Technical User
Jan 20, 2011
113
CA
Hello All;

Using Access 2007.
I have added a browse button to a form using the MS Common Dialog Control to open the browsing window and then I want a text box to display the path of the file selected. I found the code I need in a post and have copied it to my form's module. The browser window opens up just the way I want and the author says that by selecting a file and then clicking the "Open" button the path should appear in the text box. Uunfortunatley it doesn't. Here is the code - can anyone pinpoint why it wouldn't populate the field? I thought that the line Me!lbldb = VFile might be the problem as I had not named the unbound text field but have played with it for awhile and no luck.


Private Sub cmdBrowse_Click()
Dim VFile As String
On Error GoTo cmdBrowse_Click_Err
ChDrive ("C")
ChDir ("C:\")
cmDialog1.Filter = "All Files (*.*)|*.*|cmDialog1.FilterIndex = 1
cmDialog1.Action = 1
If cmDialog1.FileName = "" Then
VFile = cmDialog1.FileName
Me!lbldb = VFile
End If
cmdBrowse_Click_Exit:
Exit Sub

cmdBrowse_Click_Err:
MsgBox Err.Description, , "cmdBrowse_Click"
Resume cmdBrowse_Click_Exit
End Sub

Thanks everyone!
 
Have you single stepped the code to see what is happening?

(RG for short) aka Allan Bunch MS Access MVP acXP, ac07 ac10 - winXP Pro, Win7 Pro
Please respond to this forum so all may benefit
 
Sorry RuralGuy - I'm not sure how I do that - can you let me know the steps?
 
Click outside of the left margin and you get a dot indicating a BreakPoint. Run the form and when that code executes it will stop on the BreakPoint. Use F8 to step through the code and if you hover over a variable a box pops up with the value.

(RG for short) aka Allan Bunch MS Access MVP acXP, ac07 ac10 - winXP Pro, Win7 Pro
Please respond to this forum so all may benefit
 
Thanks RG

The line causing the problem for me was:
If cmDialog1.FileName = "" Then
Should be:
If cmDialog1.FileName <> "" Then

Here is the code that actually works in case anyone else wants to utilize a browse button to get a file path to send in an Access email form:

Private Sub cmdBrowse_Click()
On Error GoTo cmdBrowse_Click_Err
Dim VFile As String

ChDrive ("C")
ChDir ("C:\")

cmDialog1.Filter = "All Files (*.*)|*.*|Text Files (HZ*.txt)|*.txt|Excel WorkBooks (*.xls)|*.xls"

cmDialog1.FilterIndex = 1

cmDialog1.Action = 1

If cmDialog1.FileName <> "" Then
VFile = cmDialog1.FileName

Me!lbldb = VFile
End If

cmdBrowse_Click_Exit:
Exit Sub

cmdBrowse_Click_Err:
MsgBox Err.Description, , "cmdBrowse_Click"
Resume cmdBrowse_Click_Exit
End Sub
 
Excellent! ...and now you know a bit about the debug features.

(RG for short) aka Allan Bunch MS Access MVP acXP, ac07 ac10 - winXP Pro, Win7 Pro
Please respond to this forum so all may benefit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top