Solved

Save and Load picture with Access database !

Posted on 2001-06-11
4
300 Views
Last Modified: 2012-08-13
Hi there !
I am using VB to access a table from Access database, please tell me with ADO code or DataEnv ActiveX how to :

1. Load picture from access database into Picture Box control ?

2. Save picture which selected in Picture Box control into Access Database ?

Notes:     I means save itself not it's link.

Thanks so much !
0
Comment
Question by:e9608092
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
sharmon earned 200 total points
Comment Utility
If you are using ADO 2.5 or higher, you can use the stream object.  I will give you basics, if you know enough you'll be fine, if not, just ask and we'll help you.  If you are using ADO 2.1 or lower, this will not work, you'll have to use the old method, let us know and I am sure me or someone will give you that code, but I'd recommend using ADO 2.5 or higher.


This will read a steam from the database and store it as a file...replace d:\tmp.pic with whatever suits you.

If IsNull(rs.Fields("Photo").Value) Then
    Picture1.Picture = LoadPicture()
Else
    Dim adoStream As New ADODB.Stream
    adoStream.Type = adTypeBinary
    adoStream.Open
    adoStream.Write rs.Fields("Photo").Value
    adoStream.SaveToFile "d:\tmp.pic", adSaveCreateOverWrite
    Picture1.Picture = LoadPicture("d:\tmp.pic")
    adoStream.Close
    Set adoStream = Nothing
End If

If you want to write an image to the database you can use this...if you need to save your picturebox to a file first you can, but it will save as a bitmap and it will be alot bigger than a jpg or gif, so I would recommend grabbing the jpg or gif from the file instead of writing out the bitmap to the disk.

Dim adoStream As New ADODB.Stream

adoStream.Type = adTypeBinary
adoStream.Open
adoStream.LoadFromFile "d:\4.jpg"  'replace with your file

rs.AddNew
rs.Fields("Photo").Value = adoStream.Read
rs.Update

adoStream.Close
Set adoStream = Nothing
0
 
LVL 6

Expert Comment

by:sharmon
Comment Utility
Also, if you don't already know, make sure that the field type for the photo column is OLE Object, in the example I assumed rs was the name of your recordset and that Photo was the name of the column in the recordset.  Since you seem to already know about ADO other than this, I didn't figure you'd need the whole thing explained from start to finish.  If so, just ask.
0
 
LVL 6

Expert Comment

by:simonbennett
Comment Utility
Hi

My 2p

I have never used ado streaming, but it sounds similar to binary reads and writes. IMHO the only fast way to do this is to store the *path and filename* only in the database, and when you need to display it use the picture1.picture = loadpicture(<path and filename>).

The main reason for this is that your access db will increase in size very quickly, and the bigger it gets, the more likely it is to corrupt!

HTH and good luck

Simon
0
 

Author Comment

by:e9608092
Comment Utility
Hello sharmon, Simon, Thank you, now I can continue my work with your instructions.


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now