Create button to add file location of picture on form in access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • debbie1954
    New Member
    • Feb 2021
    • 9

    Create button to add file location of picture on form in access

    I am using access for storing records for our cemetery. I have a form to enter information for new interments. I would need to have a button that will take the user to the file location of the headstone picture (jpg) display the picture and save it on the form. I know next to nothing about coding so something I could copy and paste would be awesome.
    Thanks in advance
  • isladogs
    Recognized Expert Moderator Contributor
    • Jul 2007
    • 479

    #2
    Save the image file path as a field in the relevant table
    Add an image control to your form and set its control source to the file path field to display the image.
    Note that you don't 'save' the image on the form.

    Comment

    • debbie1954
      New Member
      • Feb 2021
      • 9

      #3
      I don’t know how add an image control to the form. I understand what I need to do but I don’t know how to do it.

      Comment

      • isladogs
        Recognized Expert Moderator Contributor
        • Jul 2007
        • 479

        #4
        First of all, add the file path field to your table and populate it for several/all records.
        Its quick and easy to add an image control on your form. Quicker to do than to explain.....

        1. Open the form in design view.
        2. Now go to the Controls section on the Design ribbon, click the dropdown to see all controls and click the Image control (its probably the third from last).
        3. Draw out a rectangle shape on the form for your image control and adjust size/position if needed.
        4. go to the property sheet on the right of the screen and select the image field name as its control source
        5. Save your form and return to normal form view.

        Each record will now show the image stored at that file path

        Comment

        • debbie1954
          New Member
          • Feb 2021
          • 9

          #5
          I guess I have not made it clear what I am trying to do. I have the form set up exactly as you described above and it does display the picture if there is a file location for the picture in the table. What I want to do is add a button on the form so that a person who is entering a new person's data can browse to the location where the picture is stored and select the picture file path that will then store it in the table. I originally added the location in the table by copying and pasting it there. People adding new data will not have the option to go and add it to the table manually. I want them to be able to add it from the form with a button. Hope that makes it clearer what I am trying to do. I know you have to apply a code to the button to make this happen but I don't know enough about coding to figure it out. If I could copy and paste the code into the form that would be great.
          Thanks

          Comment

          • isladogs
            Recognized Expert Moderator Contributor
            • Jul 2007
            • 479

            #6
            I did wonder .... but answered the actual question you asked!

            Code like the following on a cmdBrowse button should do what you want:

            Code:
            Private Sub cmdBrowse_Click()
            
            On Error GoTo Err_Handler
            
            'Code compatible with both 32-bit & 64-bit Office
                
            ' Set options for the file dialog box.
                Dim F As FileDialog
                Dim strFilePath As String
                Set F = Application.FileDialog(msoFileDialogFilePicker)
                F.title = "Locate the image file folder and click on 'Open' to select it"
                
            ' Clear out any current filters, and add our own
                F.Filters.Clear
                F.Filters.Add "Image files", "*.jpg;*.jpeg"
                
            ' Set the start folder. Open in default file folder if blank
                F.InitialFileName = Nz(Application.CurrentProject.path & "\ImageFiles\", "C:\") 'modify this as appropriate
                
            ' Call the Open dialog procedure.
                F.Show
            
            ' Return the path and file name.
                strFilePath = F.SelectedItems(1)
                Me.ImagePath = strFilePath  'add the file path to a textbox on the form
                
            Exit_Handler:
                Exit Sub
            
            Err_Handler:
                If Err <> 5 Then 'err=5 user cancelled
                    MsgBox "Error " & Err.Number & " in cmdBrowse_Click procedure: " & Err.Description
                End If
                Resume Exit_Handler
            
            End Sub
            Hope that helps
            Last edited by isladogs; Feb 8 '21, 08:22 PM. Reason: Add missing code line

            Comment

            • debbie1954
              New Member
              • Feb 2021
              • 9

              #7
              Do I just copy and paste that in the on click event for the button. Do I need to change anything in the code? I am not knowledgeable about coding at all.
              Thanks

              Comment

              • isladogs
                Recognized Expert Moderator Contributor
                • Jul 2007
                • 479

                #8
                You can paste it as written with a couple of minor tweaks to match your database

                First of all, note that I've just added a missing code line - Dim strFilePath As String

                As stated in the code modify the line below to match the default location for your image files
                Code:
                   F.InitialFileName = Nz(Application.CurrentProject.path & "\ImageFiles\", "C:\")
                As written, this will open the browse window to a subfolder ImageFiles in the location of your database ...if the folder exists... and if it doesn't exist, it will open to the root folder C:\

                The selected file path will be displayed in a textbox called ImagePath. If your textbox has another name, modify the line below
                Code:
                Me.ImagePath = strFilePath  'add the file path to a textbox on the form
                Strongly recommend you do a basic course in simple coding e.g. the free videos by Steve Bishop on You Tube are an excellent starting point

                Comment

                • debbie1954
                  New Member
                  • Feb 2021
                  • 9

                  #9
                  Yes a course would be excellent...per haps when I finish this project before I start the next.
                  I pasted the code
                  My textbook was called ImageFile so I didn't need to change that
                  I didn't add a specific location for the pictures opening to C:\ is fine
                  When i click on the browse button I get the following error
                  Compile error
                  User-defined type not defined
                  Thank you for helping me with this I really appreciate it.

                  Deborah

                  Comment

                  • isladogs
                    Recognized Expert Moderator Contributor
                    • Jul 2007
                    • 479

                    #10
                    Make sure you added the missing code line that I mentioned.
                    Also, there shouldn't be a space after the semicolon in this line: F.Filters.Add "Image files", "*.jpg;*.jp eg"

                    EDIT:
                    If you are getting the error on the line Dim F As FileDialog you need to add the library reference Microsoft Office xx.0 Object Library where xx is a number e.g. 16 for Access 2016/2019/365

                    See image in the link below. I can't make the image visible at the moment due to a site glitch when editing posts
                    Attached Files
                    Last edited by NeoPa; Feb 15 '21, 03:24 PM. Reason: Fixed

                    Comment

                    • debbie1954
                      New Member
                      • Feb 2021
                      • 9

                      #11
                      Thanks I figured that out and the button is working. The file path in the text box and the picture do not show up on the form until I close the form and open it again. Any ideas?

                      Deborah

                      Comment

                      • isladogs
                        Recognized Expert Moderator Contributor
                        • Jul 2007
                        • 479

                        #12
                        Both should display immediately.
                        Try the very simple example app attached which does display instantly...at least for me!

                        Assuming that works, see if you can find any differences compared to your own app.

                        If you're still stuck, make a copy of your database, remove any confidential data and all objects except the relevant form & table, compact, zip & upload it here.

                        P.S. One irritating glitch is that Access rotates jpg files counter clockwise by 90 degrees if portrait (but not if landscape). Hopefully not an issue for you. There is a solution but its quite complicated as it involves Windows APIs.
                        Attached Files

                        Comment

                        • debbie1954
                          New Member
                          • Feb 2021
                          • 9

                          #13
                          A simple problem... I did not have Name ImagePath filled in for the Text Box in properties. It work beautifully now. I have over 3000 photos in the data base and so far they have all opened properly....fin gers crossed. This is a project for the local cemetery that I have been working on for over a year. This is one of the finishing touches and I can't thank you enough for your help. I have had not training in access and am 67 years old so I am pretty happy with the results. I goggles a lot of things and found solutions but I could not seem to find help with this issue.

                          Thanks again for your patience and help!

                          Deborah

                          Comment

                          • isladogs
                            Recognized Expert Moderator Contributor
                            • Jul 2007
                            • 479

                            #14
                            You're welcome. Glad you got there in the end.
                            Perhaps you can mark the thread as answered.

                            Comment

                            • debbie1954
                              New Member
                              • Feb 2021
                              • 9

                              #15
                              I'd love to mark it as answered if I could see where to do it.

                              Comment

                              Working...