Full Version: Navigation Button To Select An Image File
UtterAccess Forums > Microsoft® Access > Access Forms
Machino
Hi All
I have taken a look and tried many ways to create navigation button and none of them seem to work the way I would like them to. I have a form that I want to add a browse button that will allow the user to call up a file browser to select a specific image file. The image filters I would like to use are JPG and PNG. I would also like, if possible, to specify the default folder location. I don't want to allow the user to select multiple files. Lastly, I would like the selected file and its path to be stored in a variable that I can show in a text box next to the browse button once the file has been selected. Any help is greatly appreciated. Thank you

Manuel A. Ayala
orange999
This material by Ryan McCormick on techblog may be helpful.

Here's info from M$oft.

Here's a youtube video that shows the general steps.
Machino
These links were very helpful but unfortunately they are written to use object libraries 14.0 or greater. I currently have 12.0
Thanks.
Manuel
doctor9
Manuel,

You can search for 2007-specific code if necessary. I did a quick Google and found this. It shouldn't be too difficult to alter for your needs.

Hope this helps,

Dennis
Machino
Hi Doctor9

I download a very similar piece of code and I adjusted it to the my requirements. I also went to the VBA-IDE and selected from the Tools>References "Microsoft Office 12.0 Access database engine Object Library" and I am getting the following error (see attached image). I am not sure what I am missing. Thanks for your help.
Manuel
doctor9
Manuel,

I don't have Access 2007, so I can't check this on my end, but the name of the reference you checked doesn't match the name of the reference in the code you took a screenshot of.

Hope this helps,

Dennis
Machino
Hi Doctor9

I am sorry I wrote the name of the other library I selected. I did select the Microsoft Access 12.0 Object Library and I am still getting that strange message. I went back and copied and pasted the original code and I am still getting the user define error message on the following bit of code: Dim fDialog As Office.FileDialog
Thanks.
Manuel
doctor9
Manuel,

As I don't have Access 2007, I can't test the code, though it is odd that code from the Microsoft site is not working. I'll see if I can find a VIP who is using (or has experience with) Access 2007 to see if they can lend a hand.

Meanwhile, I would recommend NOT customizing the example code from Microsoft and just test it as is, just in case the error is happening because of a change you're making. If the code from the page I linked you to works as it appears on their site, then the problem is with your customization.

Dennis
Machino
Hi Doctor9

After much fiddling with code, I was able to get the following to work. Now I am trying to have the selected file display next to the button. I added text box and set the control source to Sfilename and that doesn't work. Any help is greatly appreciated.
Manuel


Private Sub cmdBrowse_Click()

Dim f As Object

Set f = Application.FileDialog(3)
With f
'Allow user to make multiple selections in dialog box.
.AllowMultiSelect = False

'Set the title of the dialog box.
.Title = "Select a signature file"

'Clear out the current filters, and add our own.
.Filters.Clear
.Filters.Add "JPEG Files", "*.JPG; *.JPEG"
.Filters.Add "BMP Files", "*.BMP"
.Filters.Add "PNG Files", "*.PNG"
.Filters.Add "All Files", "*.*"
.InitialFileName = "C:\Projects\"

If .Show Then
For i = 1 To f.SelectedItems.Count
Sfilename = f.SelectedItems(i)
MsgBox Sfilename
Next
Else
Sfilename = ""
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub
R_Durrer
What is the actual error message you are receiving, if any?
doctor9
Machino,

Instead of referring to the field name from the table, try referring to the control's name instead. Also, use the prefix "Me." to specify that the object is on the form that cmdBrowse resides on. You might want to verify that the code is properly referring to the file selected by the user.

For example:

CODE
MsgBox "The number of files selected: " &  f.SelectedItems.Count
MsgBox "First filename selected: " &  f.SelectedItems(1)
Me.txtFilePathAndName = f.SelectedItems(1)


Hope this helps,

Dennis
Machino
I am not really getting an error message. After a selecting a file, I would like to show a text string next to the button that shows what was selected. This will at least let the user know that something was selected. I tried creating a textbox where I set the Control Source to Sfilename. That did not work. Thanks.
Manuel
Machino
Hi Doctor9
I added the suggested code and I am now getting the following error message - see attached image. I am not sure what i did wrong. Thanks.
Manuel
Machino
Hi All
I was finally able to get my code to work properly. Thank you for all of your assistance. I have attached what I got to work for your comments. Thank you.
Manuel


CODE
Private Sub cmdBrowse_Click()

Dim f As Object
Dim vrtSelectedItem As Variant
Dim strSelectedFile As String


Set f = Application.FileDialog(3)

With f
      'Allow user to make multiple selections in dialog box.
      .AllowMultiSelect = False
            
      'Set the title of the dialog box.
      .Title = "Select a signature file"

      'Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "JPEG Files", "*.JPG; *.JPEG"
      .Filters.Add "BMP Files", "*.BMP"
      .Filters.Add "PNG Files", "*.PNG"
      .Filters.Add "All Files", "*.*"
      '.InitialFileName = "C:\Projects\"

        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems 'onby be 1
                strSelectedFile = vrtSelectedItem
            Next vrtSelectedItem
            Me.txtSelectedFile.Caption = strSelectedFile
        Else
             Me.txtSelectedFile.Caption = ""
             MsgBox "You clicked Cancel in the file dialog box."
        End If
    End With
End Sub
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.