Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Browse button in a MS Access Form?

Page 1 of 2 12 LastLast
Results 1 to 15 of 20
Share This Thread →
  1. #1
    nic is offline Registered User
    Join Date
    Feb 2004
    Rep Power

    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.
    Attached Images Attached Images

  2. #2
    sbaxter is offline Moderator: Access, SQL
    Join Date
    Oct 2003
    Rep Power
    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)


  3. #3
    Roger_the_Pan is offline Registered User
    Join Date
    Aug 2004
    Rep Power

    Post 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)

    Sub getFileName()
    ' 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.
    End If
    End With
    End Sub

  4. #4
    ovadoggvo is offline Contributing User
    Join Date
    Oct 2004
    Rep Power

    Exclamation 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!

  5. #5
    meloncolly's Avatar
    meloncolly is offline Database junkie..
    Join Date
    May 2004
    Derry, N Ireland
    Rep Power
    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:

    '*************button code********************
    Sub open_or_save_file()
    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
    End Sub

    '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
    Option Explicit
    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
    End Type
    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 _
    If IsMissing(varDirectory) Then
    varDirectory = ""
    End If
    If IsMissing(varTitleForDialog) Then
    varTitleForDialog = ""
    End If
    ' 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( _
    OpenFile:=True, _
    InitialDir:=varDirectory, _
    Filter:=strfilter, _
    Flags:=lngFlags, _
    If Not IsNull(varFileName) Then
    varFileName = TrimNull(varFileName)
    End If
    GetOpenFile = varFileName
    End Function

    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.
    ' In:
    ' 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)
    ' Out:
    ' Return Value: Either Null or the selected filename
    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
    With OFN
    .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
    End With

    ' 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)
    End If
    ' 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 = ""
    End If
    End Function

    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
    End Function

    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
    End If
    End Function


    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.

  6. #6
    ovadoggvo is offline Contributing User
    Join Date
    Oct 2004
    Rep Power

    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


  7. #7
    sbaxter is offline Moderator: Access, SQL
    Join Date
    Oct 2003
    Rep Power
    See attachement

    Attached Files Attached Files

  8. #8
    ovadoggvo is offline Contributing User
    Join Date
    Oct 2004
    Rep Power
    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?

  9. #9
    sbaxter is offline Moderator: Access, SQL
    Join Date
    Oct 2003
    Rep Power
    The Sample I gave you displays the selectd file in a Msgbox, just put the value in that variable in your textbox


  10. #10
    ovadoggvo is offline Contributing User
    Join Date
    Oct 2004
    Rep Power
    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.

  11. #11
    pacesetter is offline Registered User
    Join Date
    Oct 2004
    Rep Power

    Cool Same Predicament

    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...

    Any takers?!


  12. #12
    sbaxter is offline Moderator: Access, SQL
    Join Date
    Oct 2003
    Rep Power

    Look at your Private Messages


  13. #13
    ovadoggvo is offline Contributing User
    Join Date
    Oct 2004
    Rep Power
    If someone makes such an app under access 2003, please forward it to me as well for reference.

  14. #14
    gkbrenne is offline Registered User
    Join Date
    Nov 2004
    Rep Power

    Thumbs up Access 2003 update

    Quote Originally Posted by ovadoggvo
    If someone makes such an app under access 2003, please forward it to me as well for reference.
    The code in 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.

    Great hint! THANK YOU ONE AND ALL!
    Attached Files Attached Files

  15. #15
    mAdMaCCa is offline Registered User
    Join Date
    Nov 2005
    Rep Power

    Red face 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

Page 1 of 2 12 LastLast
Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. Reset form button in ASP.NET
    By pixelsyndicate in forum .NET Development
    Replies: 5
    Last Post: August 2nd, 2004, 09:16 AM
  2. need help getting a button to open a form in datasheet view please
    By scaramanga in forum Microsoft Access Help
    Replies: 1
    Last Post: April 2nd, 2004, 02:01 PM
  3. Web form requires 2 submission button clicks
    By jkajen in forum .NET Development
    Replies: 3
    Last Post: March 30th, 2004, 11:59 AM
  4. update form not posting to access db
    By ephricon in forum ASP Development
    Replies: 6
    Last Post: January 7th, 2004, 08:20 AM
  5. Include more than one SUBMIT button on a form
    By Steve Schofield in forum .NET Development
    Replies: 1
    Last Post: September 12th, 2000, 04:29 AM

ASP Free Advertisers and Affiliates