Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32

    Need code for a button to browse files, and display image.

    I have a question. I need a code that will let me browse for files with a button so that I can select a path to an image, and it then shows the image in an unbound image box. If no image is selected, the box should not be visible.
    There is plenty of code that I can find online, however my problem is that they all save the entire path to the image. However if I move the database to another computer it will not find any of the images. I want the database to be in a folder with a folder called images, so that I can copy both over if needed, and it will still keep finding and showing my images.
    Help is very welcome.
    I did find this code:

    Code:
    Private Sub Form_Current() Dim ImagePath As String ImagePath = GetImagePath & Me!Filename If Len([Filename]) > 0 And Len(Dir(ImagePath)) > 0 Then Image1.Picture = ImagePath Else Image1.Picture = "" End If End Sub Public Function GetImagePath() As String GetImagePath = GetDBPath & "images\" End Function Public Function GetDBPath() As String GetDBPath = CurrentProject.Path & "\" End Function
    However, this does not include a browse button and I don't know how to change it to include it.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    So you want to only find the image and save its name but not the path? Then are you copying these images into the Images folder?

    Use FileDialog to get and save the image name into text field. http://www.ntechcomm.com/2013/08/sel...cess-with-vba/

    Create a VBA function in a general module that will pull the database file path. Then set Image control ControlSource property to:

    =GetPath() & "\Images\" & [text field]

    The function could be:

    Function GetPath() As String
    GetPath = CurrentProject.Path
    End Function

    EDIT: Did not see your code. Did you edit post and add it later?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    I get an error message while running the code from your link: Compile error: User-defined type not defined at
    Code:
    Function selectFile()
        Dim fd As FileDialog
    And yes I did add the code later. If this works that would be fine too however.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    The thread mentions adding Microsoft Office Object Library reference. Did you do that?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    Oh I overlooked that it was the OFFICE library, yes it works now after adding that.
    However, it still puts the entire path in the text field, not only the filename.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can parse the file name from a string literal like this ...

    Code:
    Dim strFileName As String
    strFileName = "\\ServerName\Folder\filename.jpg"
    strFileName = Mid(strFileName, InStrRev(strFileName, "\") + 1)
    MsgBox strFileName

  7. #7
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    And where do I put this code exactly? I now have a button linked to your code that puts the whole path into a text box.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by LadyL View Post
    And where do I put this code exactly? I now have a button linked to your code that puts the whole path into a text box.
    Are you referring to the code in post #6? The code is stand alone code that provides an example how to parse the file name from a string literal that is the full path. You can place the code behind a form as a Button Click event.

  9. #9
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    I'm sorry I still don't understand, I'm not really good at programming yet.

    I now have this button:

    Code:
    Private Sub Foto_DblClick(Cancel As Integer)Dim strPad As String 'bevat de naam van de afbeelding
    
    
    
    strPad = selectFile()
    
    
    If Not strPad = "" Then
      Me.pad = strPad
      Me.Foto.Picture = Trim(Me.pad)
      Me.Foto.Visible = True
    Else
      Me.Foto.Visible = False
    End If
    End Sub
    with this code in the module:

    Code:
    Function selectFile()    Dim fd As FileDialog
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .AllowMultiSelect = False
            If .Show Then
                selectFile = .SelectedItems(1)
            Else
                End
            End If
        End With
        Set fd = Nothing
    End Function
    Where do I put it?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    selectFile = Mid(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") + 1)

    I would not use the Picture property. This was required method before Access 2007. I use ControlSource property as described in post 2.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    Great, you are fantastic once again .

    It now inputs only the filename in the box. Strangely it now also shows the picture in the picturebox without any problems, even though I have not yet used this code:

    Code:
    =GetPath() & "\Images\" & [text field]
    
    The function could be:
    
    Function GetPath() As String
    GetPath = CurrentProject.Path
    End Function
    But I'm guessing that I do need this code in case I copy the database to another pc?
    EDIT: just in case I have just added it and it seems to work, I'll try copying it to another pc soon to make sure . In any case thank you so much again for the help.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Is your code still setting the Picture property?

    I am not sure why the image is displaying when the path was removed and not included in setting the property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    Yes that was a bit strange, however I've adapted the code now to include the code to change the file path and it seems to work. Just need to test it if I change the database folder to another pc now.

  14. #14
    LadyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2015
    Posts
    32
    I tested it and it seems it unfortunately doesn't work.
    I get an error message: Error 2220 The file cannot be opened.

    This is the code I used:


    Code:
    Private Sub Knop7_Click()Foto_DblClick (False)
    End Sub
    
    
    Private Sub Form_Current()
    If Not Trim(Me.pad) = "" Then
      Me.Foto.Picture = Trim(Me.pad)
      Me.Foto.Visible = True
    Else
      Me.Foto.Visible = False
    End If
    End Sub
    
    
    Private Sub Foto_DblClick(Cancel As Integer)
    Dim strPad As String 'bevat de naam van de afbeelding
    
    
    
    
    
    
    strPad = selectFile()
    
    
    If Not strPad = "" Then
      Me.pad = GetPath() & "\Afbeeldingen\Fotos\" & [strPad]
      Me.Foto.Picture = Trim(Me.pad)
      Me.Foto.Visible = True
    Else
      Me.Foto.Visible = False
    End If
    End Sub

    Code:
    '--------------------------------------------------
    ' File Browse Code
    '--------------------------------------------------
    'NOTE: To use this code, you must reference
    'The Microsoft Office 14.0 (or current version)
    'Object Library by clicking menu Tools>References
    'Check the box for:
    'Microsoft Office 14.0 Object Library in Access 2010
    'Microsoft Office 15.0 Object Library in Access 2013
    'Click OK
    '--------------------------------------------------
    Function selectFile()
        Dim fd As FileDialog
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        With fd
            .AllowMultiSelect = False
            If .Show Then
                selectFile = Mid(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") + 1)
            Else
                End
            End If
        End With
        Set fd = Nothing
    End Function
    Anything I did wrong? Also it asks for pictures to show in an empty database, how is this possible?

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    As noted in post 2, I would not use code to set Picture property. I would use expression in ControlSource property of Image control.

    Is this form in Continuous view? Programmatic settings of properties for controls in Continuous or Datasheet view will be reflected in ALL records.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2015, 07:13 AM
  2. Browse to import *.txt or *.xls files into table
    By Dirtpics in forum Import/Export Data
    Replies: 2
    Last Post: 06-18-2014, 10:25 AM
  3. Browse for excel files and append to Access table.
    By newbieX in forum Programming
    Replies: 5
    Last Post: 01-27-2014, 07:10 PM
  4. Button to Browse for File!
    By floyd in forum Forms
    Replies: 5
    Last Post: 08-23-2013, 09:09 AM
  5. Creating a Browse button to create a Hyperlink
    By detaylor1242 in forum Forms
    Replies: 5
    Last Post: 07-30-2013, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums