472,982 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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

9 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
Feb 7 '21 #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

17 4392
isladogs
443 Expert Mod 256MB
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.
Feb 7 '21 #2
debbie1954
9 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.
Feb 8 '21 #3
isladogs
443 Expert Mod 256MB
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
Feb 8 '21 #4
debbie1954
9 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
Feb 8 '21 #5
isladogs
443 Expert Mod 256MB
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
Feb 8 '21 #6
debbie1954
9 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
Feb 8 '21 #7
isladogs
443 Expert Mod 256MB
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
Feb 8 '21 #8
debbie1954
9 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
Feb 8 '21 #9
isladogs
443 Expert Mod 256MB
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, 1166 views)
Feb 8 '21 #10
debbie1954
9 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
Feb 8 '21 #11
isladogs
443 Expert Mod 256MB
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, 119 views)
Feb 8 '21 #12
debbie1954
9 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
Feb 8 '21 #13
isladogs
443 Expert Mod 256MB
You're welcome. Glad you got there in the end.
Perhaps you can mark the thread as answered.
Feb 8 '21 #14
debbie1954
9 Byte
I'd love to mark it as answered if I could see where to do it.
Feb 8 '21 #15
isladogs
443 Expert Mod 256MB
You've just done so. Look near the top of the thread!
Feb 8 '21 #16
NeoPa
32,548 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.
Feb 15 '21 #17
debbie1954
9 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
Feb 15 '21 #18

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

Similar topics

4
by: Lokicer | last post by:
Hi all, i want to create a file in memory and access the memory block like a file(i/o function) in windows 2000. Any hlep are appreciated. Regards, Zheng
0
by: Rob | last post by:
Is there a way to render an asp.net page, not for display in a browser, but to output the html in an htm file? I can write code that will output the desired html to a file, but it would be a lot...
10
by: robwharram | last post by:
Hi, I'm quite frustrated in the fact that I can't even display a simple "Hello World" message on .Net. I've been through all of the groups and searched all over the place and haven't been able...
3
by: The Woo | last post by:
Can one programatically create a file folder which has the same name as a key field, using a command button? Or, Can there be a a command button which opens up a directory tree to a specified...
5
by: 205210864 | last post by:
how do i create a file by a click of a button on an access form. the file i need to create is a text file with a heading and column names.
0
by: sckshreya | last post by:
i am using asp language and ms access database I need to create csv file automatically when i will press one button on my webpage. And this csv file content one table of the database. I also need to...
3
by: Johny | last post by:
Is it possible to create a file on Linux with access rights? For example owner can read and write into the file others can only read from the file Thanks for replies L.
0
by: mayormel | last post by:
Hi! This might be an odd question to ask on this forum but i have no other options. Im good in Adobe Photoshop and created many business card, flyers and so on. Now I need to create an image on PDF...
0
KalariaNitya
by: KalariaNitya | last post by:
Hello All, in windows application(vb.net) i have one button control i want to relocate that button on Second button's click event. means old location is:184, 111 i want new location :59, 375...
17
by: garfield314 | last post by:
Hello! I don't have experience in programming in Access or generally VBA, so I need to ask you this: I'm creating a new DBase in Access 2007, for an insurance office. I have created 3 levels...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.