To browse for a file there are many solutions available, but I use Bill
Wilson's class module which is freely available from:
http://community.netscape.com/n/pfx...yMessages&tsn=1&tid=22415&webtag=ws-msdevapps
Rather than storing the images in the database, which will make the file
very large, I'd recommend storing just the path to the file as a text field
in the table. The image can then be loaded into an Image control (set its
SizeMode property to Zoom) at runtime. The code to do this goes in the
form's Current event procedure like so:
GrabCursor
If Not IsNull(Me.ImagePath) Then
Me.Image1.Visible = True
Me.Image1.Picture = Me.ImagePath
Else
Me.Image1.Visible = False
End If
ReturnCursor
Where Image1 is the Image control on the form and ImagePath is the text
field. The code for a button too browse to an image file and insert its path
into the text field is:
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
' you can change the next line if you want to list other file types
' when the dialogue opens
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
You'll also need to put the following module, which I name basCursor, in
your database. This calls the Windows API to set the cursor back to its
original position after an image is loaded; otherwise it jumps to one side:
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
If your are using JPEGS you might want to suppress the progress dialogue
when each image is loaded. You can find means of doing this at:
http://www.mvps.org/access/api/api0038.htm
If you want to show the images in a report you can do it in the same way as
in the form, using the report's detail section's Print event procedure:
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
Ken Sheridan
Stafford, England