Is there a way that I can create a field on my form and in my table to attach
or insert graphics? For example, if I have a database of gemstones, I would
like to attach an image of each stone in my database as I enter in the
record. Can I do this? How?
Is there a way that I can create a field on my form and in my table to attach
or insert graphics? For example, if I have a database of gemstones, I would
like to attach an image of each stone in my database as I enter in the
record. Can I do this? How?
1. Create a text field in your table.
2. On your form, put an image control and choose one of your files.
Right click on the image control and on the Format tab, delete the text
in the Picture property. Click Yes to remove the picture from the
form.
3. Put the field you created in step 1 on the form and set it's visible
property to false.
4. Put a command button on the form, right click the button and choose
Build Event.
5. Office 2003 has a file dialog object that you can use to select
files. In order to use it, you will need to go to the Tools menu, to
References. In there, scroll down to "Microsoft Office 11 Object
Library" and check it. The libraries are in alphabetic order, so it
should be a ways down the list. Close those windows to get back to the
VBA window.
6. In the procedure for the Click event of your command button, paste:
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Prevents the user from selecting multiple files
' in the dialog box.
.AllowMultiSelect = false
' Set the title of the dialog box.
.Title = "Please Select the Picture Files"
' Clear out the current filters, and then sets filter to allow
' only picture file types
.Filters.Clear
.Filters.Add "Images", "*.jpg, *.bmp, *.gif"
' If they selected a file, put the filepath in
' as the picture property of the imgGems image control
' and store the path in your table
If .Show = True Then
imgGems.Picture = varFile
PicturePath = varFile
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
In that code, imgGems is the name of the image control you made in step
2 and PicturePath is the name of the field you made in step 1.
7. Below the End Sub at the bottom of the VBA window, paste the
following:
Private Sub Form_Current()
If Not IsNull([PicturePath]) Then
imgGems.Picture = PicturePath
Else
imgGems.Picture = ""
End If
End Sub
This will make it so that when you move from record to record it loads
the appropriate picture file. Again, imgGems is the name of the image
control you made in step 2 and PicturePath is the name of the field you
made in step 1.