INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a
Computer / IT professional?
Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Promoting, selling, recruiting, coursework and thesis posting is forbidden.
How do I use a Browse button to Open and Save files?
Posted: 21 Jul 00 (Edited 27 Sep 07)
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
'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
If Err.Number = 32755 Then
MsgBox Err.Number & ": " & Err.Description
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.
Back to Microsoft: Access Other topics FAQ Index
Back to Microsoft: Access Other topics Forum
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
- Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close