Creating a Browse button in Access

sharks13

Board Regular
Joined
May 23, 2007
Messages
88
Recently, I stumbled onto some code that is supposed to place a browse button onto a form, and a textbox with the name of the file the user selects. Before I get to the code, I'll explain exactly what I want.

Essentially, what I want is to have an "Extras" folder. This folder will hold more information for specific records in the forms. I want the user to be able to, on the click of a button, look through that folder and find the correct file. Once it's found, I want the address of that file to be stored in a textbox, and then I want the user to be able to click the textbox (hyperlink probably?) to bring up the file whenever they want.

Here is the code. I think it's basically what I want, but I can't get it to work.
Code:
Private Sub bBrowse_Click()
On Error GoTo Err_bBrowse_Click
   
    Dim strFilter As String
    Dim lngFlags As Long
    Dim varFileName As Variant

    Me.tbHidden.SetFocus

'   strFilter = "Access (*.mdb)" & vbNullChar & "*.mdb" _
'    & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"
'    strFilter = "Access Files (*.mdb)" & vbNullChar & "*.mdb*"
    strFilter = "All Files (*.*)" & vbNullChar & "*.*"

    lngFlags = tscFNPathMustExist Or tscFNFileMustExist Or tscFNHideReadOnly

    varFileName = tsGetFileFromUser( _
    fOpenFile:=True, _
    strFilter:=strFilter, _
    rlngflags:=lngFlags, _
    strInitialDir:="C:\Windows\", _
    strDialogTitle:="Find File (Select The File And Click The Open Button)")
    'remove the strInitialDir:="C:\Windows\", _ line if you do not want the Browser to open at a specific location

    If IsNull(varFileName) Or varFileName = "" Then
        Debug.Print "User pressed 'Cancel'."
        Beep
        MsgBox "File selection was canceled.", vbInformation
        Exit Sub
    Else
        'Debug.Print varFileName
        tbFile = varFileName
    End If

    Call ParseFileName

Exit_bBrowse_Click:
    Exit Sub

Err_bBrowse_Click:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_bBrowse_Click

      
End Sub

The button is of course named bBrowse and I have a textbox called tbHidden and a textbox called tbFile as well. I'm getting an error on the strDialogTitle:="Find File (Select The File And Click The Open Button)") line. Any help would be appreciated.
 
So, you did or did not copy the code into a module that can be seen in the database window? If you have the code there, then what is the error you are receiving? If it is still you can not use named parameters, then remove the names and just use the positional parameters and let us know what happens there. Whenever you get an error message, please include the text of that message, and what line of code caused the error when you post here.

Thanks!
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Here's what I did exactly. I went to the modules tab in my example database and found the module that went along with the code in my first post. I double clicked that, and it brought up the 2nd part of code I posted. I copied the entire code in the module, then I went into the modules tab of my own database, created a new module and pasted all the code in there. I then saved and exited.

The error message I'm still getting is Compile Error: Named arguments not allowed on the strDialogTitle:="Find File (Select The File And Click The Open Button)") line. I'm not sure how to use positional parameters either.

Sorry for all the trouble, I really appreciate all the help though.
 
Upvote 0
Try this instead.

File>Get External Data...Import, select the example database and import the module that way.

I think Vic's suggestion is to just not name the arguments.
Code:
varFileName = tsGetFileFromUser(True, strFilter, lngFlags, "GetFileFromUser Test (Please choose a file)")
Named arguments are useful but they aren't mandatory.:)
 
Upvote 0
To use the positional parameters, they have to be in the correct position according to how the parameters are defined by the function itself. Therefore, for the parameters you are wanting to use, here is how the line to call the function would look:
Code:
  varFileName = tsGetFileFromUser(lngFlags, "C:\Windows\", strFilter, , , ,  _
            "Find File (Select The File And Click The Open Button)", True)
This puts each parameter in the correct position as defined by the function tsGetFileFromUser().

HTH,
 
Upvote 0
sharks13

Actually I would recommend you use Vic's code, because mine's wrong and will cause a Type Mismatch error.:oops:

If mine worked at all then I would be surprised, it would probably be a fluke.:)

The reason I say this is because the parameters are optional, that's probably why named arguments were being used in the first place.

When you don't name the arguments you need to make sure they appear in the right place when you call the function.
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top