Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images in report 1

Status
Not open for further replies.

peeli

Programmer
Jun 9, 2002
15
0
0
IN
Is there any way to insert images to aaccess database field and please give me an idea , how to print a list of photos in a data report where i kept the photos in a seperate folder(not in data base),
Thanks in advance
Philip
 
you just draw controls as defined in this code and check wheather it works or not, i have tested my code and it is working. just use single field Establishment_Logo in access database with type ole object.


Option Explicit
Dim db As Database
Dim rs As Recordset
Dim fname As String
Dim nHandle As Integer
Dim lSize As Long
Dim chunk() As Byte

'Procedure: To clear Image
Private Sub cmdclear_Click()
Image1.Picture = LoadPicture("")
End Sub
'Procedure: To end the application
Private Sub cmdexit_Click()
End
End Sub

'Procedure: To retrieve picture from database
Private Sub cmdretrieve_Click()
Dim lSize As Long
Dim sPath As String
Dim sfile As String
Dim lngOffset As Long
Const conChunkSize = 32786 'retrieve picture in small packets
Dim varChunk() As Byte
nHandle = FreeFile 'assigning file
sPath = App.Path
sfile = sPath & "\output.jpg"

'define a file for binary access
Open sfile For Binary Access Write As nHandle
'setting reference to test table
Set rs = db.OpenRecordset("test")
'if there is no record given a message to user
If rs.RecordCount = 0 Then
MsgBox "No picture stored"
Exit Sub
End If
'when a record is found retrieve the size of picture
lSize = rs.Fields("Establishment_Logo").FieldSize
If lSize = 0 Then
fname = ""
Image1.Picture = LoadPicture("")
Exit Sub
End If
'redeclaring the size of the variable to the image size
ReDim varChunk(lSize)
'retrieving the picture in small packets
Do While lngOffset < lSize
varChunk() = rs.Fields(&quot;Establishment_Logo&quot;).GetChunk(lngOffset, conChunkSize)
Put nHandle, , varChunk()
lngOffset = lngOffset + conChunkSize
Loop
Close nHandle 'close the handle
Image1.Picture = LoadPicture(sfile) 'load picture

End Sub

'Procedure: to store the picture in database
Private Sub cmdstore_Click()
Set rs = db.OpenRecordset(&quot;test&quot;)
If Image1.Picture = 0 Then
MsgBox &quot;Select the background Picture&quot;
Exit Sub
End If
'when a file is selected
If fname <> &quot;&quot; Then
nHandle = FreeFile 'assigns a free handle
'open a binary file
Open fname For Binary Access Read As nHandle
lSize = LOF(nHandle) 'assign the size of picture
If nHandle = 0 Then 'if no picture is selected close handle and exit sub
MsgBox &quot;Select the background Picture&quot;
Close nHandle
Exit Sub
End If
'redeclaring chunk size as the picture size
ReDim chunk(lSize)
Get nHandle, , chunk() 'read picture chunk by chunk
End If
'when no record in database declare a new record else edit it
If rs.RecordCount = 0 Then
rs.AddNew
Else
rs.Edit
End If
'assign the chunk to field
rs.Fields(&quot;Establishment_Logo&quot;).AppendChunk (chunk())
'update database
rs.Update
MsgBox &quot;Stored in database successfully&quot;
End Sub

Private Sub Form_Load()
'set reference to database
Set db = OpenDatabase(App.Path & &quot;\Sample.mdb&quot;)

End Sub
'select the picture
Private Sub cmdselectlogo_Click()
CD.Filter = &quot;Pictures (*.bmp;*.ico;*.jpg;*.gif)|*.bmp;*.ico;*.jpg;*.gif|&quot;
CD.ShowOpen
'assign the picture name to fname
fname = CD.FileName
'load picture to image1
Image1.Picture = LoadPicture(fname)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top