Inserting a bitmap!!!Help

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

Guest

I have limited experience with a database. This is the first one I have
built. In one of my fields I want to insert a image. The title of this is
called drawings. It is a map of certain valve locations. When I originally
built the data base I got all this to work, since we had a crash something
happened to the data base and know none of my drawing show up and I am unable
to insert the drawings for that particle address. I have tried everything
except the right thing. I do not know if it should be bound unbound or what,
Everything I try the same drawing shows up on each address. Any help would be
appreciated. Each address has it's own drawing.

Thanks Scott
 
Scott:

Storing images as objects in an Access database is not very efficient. A
better approach is to store the paths to the image files in a text field in a
table and load them into an Image control on a form and/or report at runtime.
The following is my standard method for doing this, and includes the
relevant code for a form's and report's modules. It also includes the code
for a few modules you'd need to paste into your database. One of these opens
a common dialog to allow you to browse to the image file and insert its path
into the text field.

And be sure to back up your database to a safe medium very regularly!

Good luck,

Ken Sheridan
Stafford, England

''''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 has 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 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 should 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 changes 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'''''<<
 
Your table should have a field defined as an Ole Object type. This will be
where the image is stored. In your form, use a Bound Object Frame and set the
control source to the table's Ole Object field. For each record, you should
now be able to see the stored image or right-click the frame to insert images.

Barry
 
Ok I have a bound objectframe that I paste into but it shows up on every
record. I am not sure how you set the control sourece right now the controp
source is set to drawings which is my header. Where you set control source
there is nothing that states OLE object field. How do I create this?
 
Ken thanks for your advice but my first sentence was I have limited
experience and even though I am confident your suggestion would work it is
totally Greek to me what you suggested..Sorry
 
scott said:
Ok I have a bound objectframe that I paste into but it shows up on every
record. I am not sure how you set the control sourece right now the controp
source is set to drawings which is my header. Where you set control source
there is nothing that states OLE object field. How do I create this?

Your field is set to Ole Object type in your table, not in your form.

In your form, you need to put the Bound Object Frame in the Detail section,
not one of the headers. When you select the Bound Object Frame, go to the
Data tab of the property sheet. You'll see the control source property at the
top. In that property, select the table field that you set to Ole Object.

FWIW, I agree with Ken's suggestion that storing pictures in Access can
cause significant bloat. I would use this with caution, especially if you
plan to store a lot of images. It is usually better to store the images as
files on a drive somewhere and store the path to those files in Access.

Barry
 
Barry

Like I said I am new at database and I am lost on your advice. I even use
the create link instead of pasting a picture directly into the data base and
it says The value you entered isn't valid for the field. nI have everything
in the detali section but just can not get it to work, I know I am probably
just one click away but need help.
thanks Scott
 
Ken

I am new to data base's and I will admit it I am loss on how to get even my
bitmaps to link to my page. I went to insert object choose the link choose
my bitmap and it comes back that The value you entered isn't valid for this
field. I hate to ask but I need a step by step instructions on how to do this
if you have the time.

Thanks Scott
and rememeber I am fairly new at data base.
 
Barry

Hey I finally got to the correct area on the data base. My drwaing field was
set to text I changed it to OLE and everything works. Here is a question.
This data base is stored on one pc. Is there a way to protect it so that no
one changes anything.

Thanks again Scott
 
scott said:
Barry

Hey I finally got to the correct area on the data base. My drwaing field was
set to text I changed it to OLE and everything works. Here is a question.
This data base is stored on one pc. Is there a way to protect it so that no
one changes anything.

Great! I'm glad you got it working.

Protecting the mdb from changes? Do you mean the data, the design?

If you don't want anyone to be able to get into it at all, you can password
protect the whole thing using Tools, Security, Set Database Password. This
will prompt for a single password when the mdb is opened.

If you want to prevent design changes but allow data changes or allow/prvent
changes on a more granular level, you could apply user-level security. This
allows you to specify individual users, assign them to roles, and
allow/disallow different rights to different roles/users for each database
object (form, report, table, etc.). This is a fairly big topic, so I would
suggest reading up on it in Access help. The simplest way to get started is
by running the User-Level Security Wizard under Tools, Security.

Barry
 
Hi Barry
i too am having issues. On my table the field is set to Ole Object
On the table cell i choose insert ole object then create file and lick to a
bitmap image in a directory on my pc. However in the cell field the word
package comes up and when i try to vien on the form it shows me an icon thats
it. Please please help i am so lost now..
Lore
 
Back
Top