Attaching graphics to a record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
The most efficient way is to store the path to each image file in a text
field in the table and load the image into an Image control on a form or
report at runtime. The following is the code form a simple form's module
which does this:

''''module starts''''
Option Compare Database
Option Explicit

Private Sub cmdAddImage_Click()

On Error GoTo Err_Handler

Dim OpenDlg As New BrowseForFileClass
Dim strPath As String
Dim strAdditionalTypes As String, strFileList As String

' grab position of cursor
GrabCursor

strFileList = "*.bmp; *.jpg"
strAdditionalTypes = "Image Files (" & strFileList & ") |" & strFileList

' force form to Dirty
Me.ImageTitle = Me.ImageTitle

' open common 'file open' dialogue and get path to selected file
OpenDlg.DialogTitle = "Select Image File"
OpenDlg.AdditionalTypes = strAdditionalTypes
strPath = OpenDlg.GetFileSpec
Set OpenDlg = Nothing

' if file selected then set Picture property of Image control
' to path to file and enable ImageTitle control
If Len(strPath) > 0 Then
Me.ImagePath = strPath
Me.Image1.Picture = strPath
Me.Image1.Visible = True
End If


Exit_here:
' reset cursor position
ReturnCursor
Exit Sub

Err_Handler:
Select Case Err.Number
Case 2001
Resume
Case Else
MsgBox Err.Description, vbExclamation, "Error"
Resume Exit_here
End Select

End Sub


Private Sub cmdDeleteImage_Click()

Me.ImagePath = Null
Me.Image1.Visible = False

End Sub

Private Sub cmdReport_Click()

On Error Resume Next
DoCmd.OpenReport "rptImages", acViewPreview

End Sub

Private Sub Form_Current()

GrabCursor

If Not IsNull(Me.ImagePath) Then
Me.Image1.Visible = True
Me.Image1.Picture = Me.ImagePath
Else
Me.Image1.Visible = False
End If

ReturnCursor

End Sub
''''module ends'''''

The form includes the following controls:

1. ImagePath Text box bound to the ImagePath field in the table. The
Visible property of this can be set to True (yes in the properties sheet) to
hide it if you wish.
2. Image1 Image control. When this is added to the form in design
view its Picture property's value should be deleted in the control's
properties sheet before saving the form as this is set in code at runtime.
3. cmdAddImage Command button to add or change image attached to record.
4. cmdDeleteImage Command button to de-attach image from current record.
5. cmdReport Command button to open report.

The report also includes an image control, Image1, in its detail section.
The report's module is:

''''module starts''''
Option Compare Database
Option Explicit

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)

If Not IsNull(Me.ImagePath) Then
Me.Image1.Picture = Me.ImagePath
Me.Image1.Visible = True
Else
Me.Image1.Visible = False
End If

End Sub


Private Sub Report_Close()

DoCmd.Restore

End Sub

Private Sub Report_NoData(Cancel As Integer)

MsgBox "No data to report", vbInformation, "Images"
Cancel = True

End Sub

Private Sub Report_Open(Cancel As Integer)

DoCmd.Maximize

End Sub
''''module ends''''

The code also draws upon the following modules which sjould be pasted into
your database, the first, freely distributed by Bill Wilson and slightly
amended by me, to browse to an image file, the second to move the cursor back
to its original position as it otherwise chnges position when an image is
loaded:

''''module starts''''
Option Compare Database
Option Explicit


' Note: This module is a modified copy of part of the modRefreshLinks
' module in the Solutions database that is supplied with Access.
'
' That module in the Solutions database contains a lot of descriptive
' comments about the OPENFILENAME data structure that is used below, and
' it contains definitions for a list of constants that can be used in the
' Flags field in the OPENFILENAME data structure.
'
' This version was created by Bill Wilson in January 1999.
' E-mail: (e-mail address removed)
' Modified by Ken Sheridan, May 1999 to allow multiple 'additional types'
'
' The purpose of this class is to activate a dialog box that the User will
' use to pick out a particular file. The VBA code that uses this class can
' either use it to open a file or to just save the complete path and filename
' for a file which will be used at some future time.
'
' NB The dialog does not actually open the file. It only returns the path
' to the file for use in code (comment added by KWS).
'
' There are default values for the dialog box title and the list of file
types
' in the 'file filter' section of the dialog box. The calling VBA code can
' use the following Properties and Methods of this class.
'
' Properties:
' DialogTitle -- the text that is displayed as the title of the
' dialog box. The default is "Browse For a File".
' AdditionalTypes -- one or more additional file types to be added
as
' one item in the dialog box's file filter list,
' formatted like this sample:
' "My Files (*.mf1;*.mf2) | *.mf1;*.mf2 |
Your Files (*.yf1;*.yf2) *.yf1;*.yf2"
' The following file types are in the built-in
list:
' "All Files (*.*)"
' "Text Files (*.txt;*.prn;*.csv)"
' "Word Documents (*.doc)"
' "Word Templates (*.dot)"
' "Rich Text Files (*.rtf)"
' "Excel Files (*.xls)"
' "Databases (*.mdb)"
' "HTML Documents (*.html;*.htm)"
' DefaultType -- the item in the dialog's file filter list that
will be
' active when the dialog box is activated. If the
' AdditionalTypes property is not used, the default
' is "All files (*.*)". If the AdditionalTypes
property
' is used, this property cannot be used and the
file type
' specified in the AdditionalTypes property will be
active
' when the dialog box is activated. To set this
property,
' specify a string that will match with the desired
type,
' such as "*.doc" or "HTML".
' InitialFile -- the file name that is to be displayed in the File
Name
' field in the dialog box when it is activated. The
' default is to leave the File Name field blank.
' InitialDir -- the directory/folder which should be active when the
' dialog box is activated. The default is the current
' directory.
'
' Methods:
' GetFileSpec() -- this function activates the dialog box and then
returns
' the full path and filename of the file that the
User
' has selected. If the User clicks Cancel, a zero
' length string is returned.
'


Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As Long
nMaxCustrFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustrData As Long
lpfnHook As Long
lpTemplateName As Long
End Type

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Boolean

Private strDialogTitle As String
Private intDefaultType As Integer
Private strNewTypes As String
Private strInitialFile As String
Private strInitialDir As String
Private strFilter As String
Private strFltrLst As String
Private strFltrCnt As String
''''module ends'''''

''''module starts'''''
Option Compare Database
Option Explicit

Type POINTAPI
X As Long
Y As Long
End Type

Declare Function GetCursorPos Lib "User32" (lpPoint As POINTAPI) As Long

Declare Function SetCursorPos Lib "User32" (ByVal X As Long, ByVal Y As
Long) As Long

Public lngCursorX As Long, lngCursorY As Long

Public Sub GrabCursor()

Dim dl As Long
Dim pt As POINTAPI

dl = GetCursorPos(pt)
lngCursorX = pt.X
lngCursorY = pt.Y

End Sub

Public Sub ReturnCursor()

SetCursorPos lngCursorX, lngCursorY

End Sub
''''module ends'''''

Ken Sheridan
Stafford, England
 
Annemarie ha scritto:
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?

Look at this:

http://www.alessandrobaraldi.it/DettaglioFaq.asp?IdFAQ=117

It's in Italian language, but simply to understand.

The right way to link any image to each record is to use a TextField on
a Table that link the Path of your image.

I suppose to name the field=[FieldPathImage] and on the form you need a
control bound to the field, i also suppose to name the
control(txtBox)=[FieldPathImage]

How to show it :
You need of Image Control and On Current Form's Event :

Me!imgControlName.Picture=Me!FieldPathImage

@Alex
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top