March 3rd, 2004, 05:12 AM
Browse button in a MS Access Form?
Below is a pic of the form I'm having trouble with. the poject is a database of golf courses. there is a form for entering/viewing information for each hole for each course. basically each record has a thumbnail pic of the hole.
That browse button needs to do something. It needs to open a browse dialog box, similar to the browse dialog box you get on many other programs. The idea is that the user selects the picture from the browse dialog box and the file name of this picture is put in the PhotoFile field.
I have no idea where to start on this, but I did check out the Northwind database that comes with Access - its similar but has loads of other functions bundled with it that I don't need and doesn't work without these functions.
I'm no programming genius either! so any help is greatly appreciated.
March 3rd, 2004, 10:43 AM
To open a dialog box in Access you need to use an API Call. I can't find an example of some code I have (maybe it is a home). If I remember, when I get home, I will post it. Maybe someone else has some code(trying searching the form for "comdlg32.dll", the dll you will need)
August 18th, 2004, 05:52 PM
Code from Northwind
Here is the code from the Northwind database:
MsoFileDialogType can be one of these MsoFileDialogType constants:
msoFileDialogOpen = 1
msoFileDialogSaveAs = 2
msoFileDialogFilePicker = 3
msoFileDialogFolderPicker = 4
Some properties are not supported, so comment-out those lines with a '
(I tried a SaveAs box, and it wouldn't let me use the ".Filter" commands)
' Displays the Office File Open dialog to choose a file name
' for the current employee record.
Dim fileName As String
Dim result As Integer
With Application.FileDialog(3) ' 3 is a constant: msoFileDialogFilePicker
.Title = "Select Employee Picture"
.Filters.Add "All Files", "*.*"
.Filters.Add "JPEGs", "*.jpg"
.Filters.Add "Bitmaps", "*.bmp"
.FilterIndex = 3
.AllowMultiSelect = False
.InitialFileName = CurrentProject.path
result = .Show
If (result <> 0) Then 'result = 0 if nothing was selected
fileName = Trim(.SelectedItems.Item(1))
'filename contains the path you want.
October 7th, 2004, 01:26 AM
I need the same function, but the above script is not working
I need a script that i can set to a button that will output the path and the filename to a certain field in a form.
Please let me know!
October 7th, 2004, 02:25 AM
i think the previously posted code only works in later versions of access, but here's a method that works (as far as I can tell) with all versions:
place the second portion of code in a module, and call it by using code like this behind a button:
Dim strfilter As String
Dim strSaveFilename As String
strfilter = ahtAddFilterItem("Excel Files (*.xls)", "*.xls")
strSaveFilename = ahtCommonFileOpenSave(OpenFile:=False, Filter:=strfilter, _
Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY) 'set OpenFile to true if selecting a file to open
'strSaveFilename is then stored in memory - it's the path and filename
'you an then put this in a textbox
'me.txtMyFileName = strSaveFilename
'*******place ALL of the following in a module************
Option Compare Database
lStructSize As Long
hwndOwner As Long
hInstance As Long
strfilter As String
strCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
strfile As String
nMaxFile As Long
strFileTitle As String
nMaxFileTitle As Long
strInitialDir As String
strTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _
Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Global Const ahtOFN_READONLY = &H1
Global Const ahtOFN_OVERWRITEPROMPT = &H2
Global Const ahtOFN_HIDEREADONLY = &H4
Global Const ahtOFN_NOCHANGEDIR = &H8
Global Const ahtOFN_SHOWHELP = &H10
Global Const ahtOFN_NOVALIDATE = &H100
Global Const ahtOFN_ALLOWMULTISELECT = &H200
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
Global Const ahtOFN_PATHMUSTEXIST = &H800
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
Global Const ahtOFN_NOTESTFILECREATE = &H10000
Global Const ahtOFN_NONETWORKBUTTON = &H20000
Global Const ahtOFN_NOLONGNAMES = &H40000
' New for Windows 95
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000
Function GetOpenFile(Optional varDirectory As Variant, _
Optional varTitleForDialog As Variant) As Variant
' Here's an example that gets an Access database name.
Dim strfilter As String
Dim lngFlags As Long
Dim varFileName As Variant
' Specify that the chosen file must already exist,
' don't change directories when you're done
' Also, don't bother displaying
' the read-only box. It'll only confuse people.
lngFlags = ahtOFN_FILEMUSTEXIST Or _
ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
If IsMissing(varDirectory) Then
varDirectory = ""
If IsMissing(varTitleForDialog) Then
varTitleForDialog = ""
' Define the filter string and allocate space in the "c"
' string Duplicate this line with changes as necessary for
' more file templates.
strfilter = ahtAddFilterItem(strfilter, _
"Access (*.mdb)", "*.MDB;*.MDA")
' Now actually call to get the file name.
varFileName = ahtCommonFileOpenSave( _
If Not IsNull(varFileName) Then
varFileName = TrimNull(varFileName)
GetOpenFile = varFileName
Function ahtCommonFileOpenSave( _
Optional ByRef Flags As Variant, _
Optional ByVal InitialDir As Variant, _
Optional ByVal Filter As Variant, _
Optional ByVal FilterIndex As Variant, _
Optional ByVal DefaultExt As Variant, _
Optional ByVal FileName As Variant, _
Optional ByVal DialogTitle As Variant, _
Optional ByVal hwnd As Variant, _
Optional ByVal OpenFile As Variant) As Variant
' This is the entry point you'll use to call the common
' file open/save dialog. The parameters are listed
' below, and all are optional.
' Flags: one or more of the ahtOFN_* constants, OR'd together.
' InitialDir: the directory in which to first look
' Filter: a set of file filters, set up by calling
' AddFilterItem. See examples.
' FilterIndex: 1-based integer indicating which filter
' set to use, by default (1 if unspecified)
' DefaultExt: Extension to use if the user doesn't enter one.
' Only useful on file saves.
' FileName: Default value for the file name text box.
' DialogTitle: Title for the dialog.
' hWnd: parent window handle
' OpenFile: Boolean(True=Open File/False=Save As)
' Return Value: Either Null or the selected filename
Dim OFN As tagOPENFILENAME
Dim strFileName As String
Dim strFileTitle As String
Dim fResult As Boolean
' Give the dialog a caption title.
If IsMissing(InitialDir) Then InitialDir = CurDir
If IsMissing(Filter) Then Filter = ""
If IsMissing(FilterIndex) Then FilterIndex = 1
If IsMissing(Flags) Then Flags = 0&
If IsMissing(DefaultExt) Then DefaultExt = ""
If IsMissing(FileName) Then FileName = ""
If IsMissing(DialogTitle) Then DialogTitle = ""
If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
If IsMissing(OpenFile) Then OpenFile = True
' Allocate string space for the returned strings.
strFileName = Left(FileName & String(256, 0), 256)
strFileTitle = String(256, 0)
' Set up the data structure before you call the function
.lStructSize = Len(OFN)
.hwndOwner = hwnd
.strfilter = Filter
.nFilterIndex = FilterIndex
.strfile = strFileName
.nMaxFile = Len(strFileName)
.strFileTitle = strFileTitle
.nMaxFileTitle = Len(strFileTitle)
.strTitle = DialogTitle
.Flags = Flags
.strDefExt = DefaultExt
.strInitialDir = InitialDir
' Didn't think most people would want to deal with
' these options.
.hInstance = 0
.strCustomFilter = ""
.nMaxCustFilter = 0
.lpfnHook = 0
'New for NT 4.0
.strCustomFilter = String(255, 0)
.nMaxCustFilter = 255
' This will pass the desired data structure to the
' Windows API, which will in turn it uses to display
' the Open/Save As Dialog.
If OpenFile Then
fResult = aht_apiGetOpenFileName(OFN)
fResult = aht_apiGetSaveFileName(OFN)
' The function call filled in the strFileTitle member
' of the structure. You'll have to write special code
' to retrieve that if you're interested.
If fResult Then
' You might care to check the Flags member of the
' structure to get information about the chosen file.
' In this example, if you bothered to pass in a
' value for Flags, we'll fill it in with the outgoing
' Flags value.
If Not IsMissing(Flags) Then Flags = OFN.Flags
ahtCommonFileOpenSave = TrimNull(OFN.strfile)
ahtCommonFileOpenSave = ""
Function ahtAddFilterItem(strfilter As String, _
strDescription As String, Optional varItem As Variant) As String
' Tack a new chunk onto the file filter.
' That is, take the old value, stick onto it the description,
' (like "Databases"), a null character, the skeleton
' (like "*.mdb;*.mda") and a final null character.
If IsMissing(varItem) Then varItem = "*.*"
ahtAddFilterItem = strfilter & _
strDescription & vbNullChar & _
varItem & vbNullChar
Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
intPos = InStr(strItem, vbNullChar)
If intPos > 0 Then
TrimNull = Left(strItem, intPos - 1)
TrimNull = strItem
If you found a post particularly helpful, show your appreciation by clicking the "scales" icon in the bar just above the post, at the right hand side.
October 7th, 2004, 02:12 PM
Cant Get it to work
I can't get it to work for some reason.. Can you email me a sample database with this function working in a simple form with one textbox and a browse button. Maybe i need to see an application of it in order to be able to get it to work for me. I'm running access 2003 from office 2003 professional suite.
IM me using AIM .. sn= ovadoggvo
October 7th, 2004, 04:04 PM
October 7th, 2004, 09:55 PM
Thanks for the help.. i see where i made the mistake. How would i have the file i select be inserted into a textbox on that same form?
October 7th, 2004, 11:35 PM
The Sample I gave you displays the selectd file in a Msgbox, just put the value in that variable in your textbox
October 11th, 2004, 06:04 PM
The code works in the database you sent me, but when i import the modules and form into another database it gets a compile error at Debug.Print Hex(lngFlags)
.. Why is that? I have tried importing all the modules and the form itself, and i still experience the same error. I have also tried converting the file to access 2003.. once converted it works, but when i try to import it to my own mdb it gets the same error.
Let me know.
October 13th, 2004, 07:17 AM
I too am in the same pridicament...
I have tried about a dozen ways to do it, but I still can not get it to work properly. I am looking to have my form have 4 textboxes, all with Browse buttons next to them. Upon clicking the Browse button, they select a JPG photo, and then that filename and location is written into the textbox, then to the database.
If someone could quickly make an example database, with 1 table and these 4 fields in it, the form which allows you to browse for photos, and a report that gets the photos based on the filename and location, and actually imports the picture into the report, I am willing to send $25 through PayPal, and have the files avaliable for the rest of the community. I know its not much, maybe half an hours work, but I can not seem to make it happen and the patience is gone...
October 13th, 2004, 02:51 PM
Look at your Private Messages
October 14th, 2004, 12:18 PM
If someone makes such an app under access 2003, please forward it to me as well for reference.
November 16th, 2004, 05:51 PM
Access 2003 update
The code in samplenew.zip works just fine in Access 2003 with no changes. I've attached a zip file of just the VBS module. In the initial comments, I've included which references were turned on and how to create the form.
Originally Posted by ovadoggvo
Great hint! THANK YOU ONE AND ALL!
November 10th, 2005, 04:01 PM
Works Great but...
Hi, I'm new to the forum and would firstly like to say thanx for this solution.
Initially it seemed to work fine, but on closer inspection I have found something I didn't expect...
I am using the routine with OpenFile set to True so that it only opens files. The filter box displays just the expected Excel Files (*.xls) *.xls, however, in the folder/files box ALL file types are listed/viewable and therefore selectable.
Has anyone else had this problem? Does anyone know how to only allow the filtered file type to be viewed and so selected and opened?
Any help would be very much appreciated.
Sorry, forgot to mention that i'm using Access 2003. Just hoping anyone can remember this post it's that old and help