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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use a Browse button to Open and Save files?

Access Howto:

How do I use a Browse button to Open and Save files?

by  jimmythegeek  Posted    (Edited  )
This example uses the Microsoft ActiveX control. The Access Developers handbook has another example.

1) On your form, add a common dialog control
(Insert | ActiveX Control) (Microsoft common dialog control, version 5)
2) Name the control "cdlgOpen"
3) Add a button
4) Name the button "cmdBrowse"
5) Optional: I use either a private or public constant for my default path if you want one.

In General Declaration Section of form (private) or module (public):

Private Const EXISTINGFILE As String = "C:\My Documents\Zips\tasks.xls"
Private Const EXISTINGDIR AS String = "C:\MyDocuments\"

5) Copy and paste the following code on the Click Event of the button (minus the Private & End Sub lines).

==========
Private Sub cmdBrowse_Click()
Dim strNewFile As String

On Error GoTo Browse_Err
With Me.cdlgOpen
'You should only use one of the next two lines if you want to open to either a default file or folder
.filename = EXISTINGFILE 'Comment out if you don't want to open to a default file
'.initdir = ESIXTINGDIR 'Comment out if you don't want to open to a default folder
.CancelError = True
.Filter = "Excel Files (*.xls)|*.xls|" 'Can change (i.e. "Word Files (*.doc)|*.doc|") or ("All Files (*.*)|*.*|") see help for other examples
.ShowOpen ' Can use showSave for saving file, same principle
strNewFile = .filename
End With

Browse_Exit:
Exit Sub

Browse_Err:
If Err.Number = 32755 Then
Resume Browse_Exit
Else
MsgBox Err.Number & ": " & Err.Description
Resume Browse_Exit
End If
End Sub
==========

6) Now your variable strFile will represent your file and you can do whatever you want with it at that point.

If your default paths have the possibility of changing, you might consider putting them into a table or .ini file and retrieving them. This will allow you to change the path without having to adjust your code.

==========================

Jim Lunde
jimlunde@gmail.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top