467,926 Members | 1,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

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

Byte
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
4 Weeks Ago #1

✓ answered by isladogs

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

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdBrowse_Click()
  2.  
  3. On Error GoTo Err_Handler
  4.  
  5. 'Code compatible with both 32-bit & 64-bit Office
  6.  
  7. ' Set options for the file dialog box.
  8.     Dim F As FileDialog
  9.     Dim strFilePath As String
  10.     Set F = Application.FileDialog(msoFileDialogFilePicker)
  11.     F.title = "Locate the image file folder and click on 'Open' to select it"
  12.  
  13. ' Clear out any current filters, and add our own
  14.     F.Filters.Clear
  15.     F.Filters.Add "Image files", "*.jpg;*.jpeg"
  16.  
  17. ' Set the start folder. Open in default file folder if blank
  18.     F.InitialFileName = Nz(Application.CurrentProject.path & "\ImageFiles\", "C:\") 'modify this as appropriate
  19.  
  20. ' Call the Open dialog procedure.
  21.     F.Show
  22.  
  23. ' Return the path and file name.
  24.     strFilePath = F.SelectedItems(1)
  25.     Me.ImagePath = strFilePath  'add the file path to a textbox on the form
  26.  
  27. Exit_Handler:
  28.     Exit Sub
  29.  
  30. Err_Handler:
  31.     If Err <> 5 Then 'err=5 user cancelled
  32.         MsgBox "Error " & Err.Number & " in cmdBrowse_Click procedure: " & Err.Description
  33.     End If
  34.     Resume Exit_Handler
  35.  
  36. End Sub
  37.  
Hope that helps

  • viewed: 2417
Share:
17 Replies
isladogs
Expert 128KB
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.
4 Weeks Ago #2
Byte
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.
4 Weeks Ago #3
isladogs
Expert 128KB
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
4 Weeks Ago #4
Byte
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
4 Weeks Ago #5
isladogs
Expert 128KB
I did wonder .... but answered the actual question you asked!

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdBrowse_Click()
  2.  
  3. On Error GoTo Err_Handler
  4.  
  5. 'Code compatible with both 32-bit & 64-bit Office
  6.  
  7. ' Set options for the file dialog box.
  8.     Dim F As FileDialog
  9.     Dim strFilePath As String
  10.     Set F = Application.FileDialog(msoFileDialogFilePicker)
  11.     F.title = "Locate the image file folder and click on 'Open' to select it"
  12.  
  13. ' Clear out any current filters, and add our own
  14.     F.Filters.Clear
  15.     F.Filters.Add "Image files", "*.jpg;*.jpeg"
  16.  
  17. ' Set the start folder. Open in default file folder if blank
  18.     F.InitialFileName = Nz(Application.CurrentProject.path & "\ImageFiles\", "C:\") 'modify this as appropriate
  19.  
  20. ' Call the Open dialog procedure.
  21.     F.Show
  22.  
  23. ' Return the path and file name.
  24.     strFilePath = F.SelectedItems(1)
  25.     Me.ImagePath = strFilePath  'add the file path to a textbox on the form
  26.  
  27. Exit_Handler:
  28.     Exit Sub
  29.  
  30. Err_Handler:
  31.     If Err <> 5 Then 'err=5 user cancelled
  32.         MsgBox "Error " & Err.Number & " in cmdBrowse_Click procedure: " & Err.Description
  33.     End If
  34.     Resume Exit_Handler
  35.  
  36. End Sub
  37.  
Hope that helps
4 Weeks Ago #6
Byte
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
4 Weeks Ago #7
isladogs
Expert 128KB
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
Expand|Select|Wrap|Line Numbers
  1.    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
Expand|Select|Wrap|Line Numbers
  1. 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
4 Weeks Ago #8
Byte
Yes a course would be excellent...perhaps 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
4 Weeks Ago #9
isladogs
Expert 128KB
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;*.jpeg"

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 Images
File Type: png Capture.PNG (19.0 KB, 182 views)
4 Weeks Ago #10
Byte
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
4 Weeks Ago #11
isladogs
Expert 128KB
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
File Type: zip SimpleImageControl.zip (33.0 KB, 10 views)
4 Weeks Ago #12
Byte
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....fingers 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
4 Weeks Ago #13
isladogs
Expert 128KB
You're welcome. Glad you got there in the end.
Perhaps you can mark the thread as answered.
4 Weeks Ago #14
Byte
I'd love to mark it as answered if I could see where to do it.
4 Weeks Ago #15
isladogs
Expert 128KB
You've just done so. Look near the top of the thread!
4 Weeks Ago #16
NeoPa
Expert Mod 16PB
Debbie1954:
... and am 67 years old ...
Who would have guessed? With a user name like that ;-)

I've reset the Best Answer. No criticism for your choice. It reflects that IslaDogs helped you. However, we also prefer it to be the item that, alone, is most helpful for anyone else researching such a question.

Let me congratulate you on your rare clarity of expression. You may not have a lot of technical understanding or experience, yet you were always able to express where you were coming from without ambiguity. A rare pleasure to read. It also made helping you much easier than it could have been.
3 Weeks Ago #17
Byte
Thank you for that. I knew it was on the wrong spot but was not sure how to fix it. Thank you for doing that. I have completed my project which would not have turned out as good as it did without the help from here. I am deeply appreciative.

Debbie
3 Weeks Ago #18

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Lokicer | last post: by
reply views Thread by Rob | last post: by
3 posts views Thread by Johny | last post: by
reply views Thread by mayormel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.