VB Add/Edit/Save Pictures in Access DB
Hi everyone, is it possible to add/edit/save picture in MS Access DB using ADO, I have this code
If (MsgBox("Are you sure you entered correct data?" & vbNewLine & "Save now?", vbYesNo + vbQuestion, "Confirm Save") = vbYes) Then
squery = "": squery = "SELECT * FROM tblEmp"
Call ExecuteCommand
Set rs = New ADODB.Recordset
With rs
.Open squery, conn, adOpenStatic, adLockPessimistic
.AddNew
.Fields("EmpID") = txtEmpID.Text
.Fields("LName") = txtLast.Text
.Fields("FName") = txtFirst.Text
.Fields("MName") = txtMiddle.Text
.Fields("Pos") = txtPos.Text
.Update
End With
I want also to add/save picture together with this data, using Image or PictureBox.
Then I want to retrieve it later on and display the picture within the Form based on the user's QUERY. then is it also possible to display this picture in Data Report when the user click the command PREVIEW.
Thank you very much for your help.
I will highly appreciate it.
arvin2006
Junior Poster in Training
69 posts since Sep 2006
Reputation Points: 15
Solved Threads: 4
please try to use the sample code to store picture.
this sample code uses ADO for database connection
Dim CN As New ADODB.Connection
Dim RS As ADODB.Recordset
Dim DataFile As Integer, Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
Private Const ChunkSize As Integer = 16384
Private Const conChunkSize = 100
Private Sub cmdSave_Click()
CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
Dim strSQL As String
strSQL = "SELECT * FROM pub_info where pub_id = '9999'"
RS.Open strSQL, CN, adOpenForwardOnly, adLockOptimistic
RS.AddNew
SavePicture
RS.Update
Set RS = Nothing
Set RS = New Recordset
End Sub
Private Sub SavePicture()
Dim strFileNm As String
DataFile = 1
Open strFileNm For Binary Access Read As DataFile
Fl = LOF(DataFile) ' Length of data in file
If Fl = 0 Then Close DataFile: Exit Sub
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
ReDim Chunk(Fragment)
Get DataFile, , Chunk()
RS!logo.AppendChunk Chunk()
ReDim Chunk(ChunkSize)
For i = 1 To Chunks
Get DataFile, , Chunk()
RS!logo.AppendChunk Chunk()
Next i
Close DataFile
End Sub
debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
please try to use the sample code to retrieve picture from database tables.
again this sample code uses ADO for database connection.
Dim CN As New ADODB.Connection
Dim RS As ADODB.Recordset
Dim DataFile As Integer, Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, i As Integer, FileName As String
Private Const ChunkSize As Integer = 16384
Private Const conChunkSize = 100
Private Sub Form_Load()
CN.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Pubs;Data Source=Test"
Dim strsql As String
strsql = "SELECT * FROM pub_info where pub_id = '9999'"
RS.Open strsql, CN, adOpenForwardOnly, adLockReadOnly
ShowPic
Set RS = Nothing
Set RS = New Recordset
End Sub
Private Sub ShowPic()
DataFile = 1
Open "pictemp" For Binary Access Write As DataFile
Fl = RS!logo.ActualSize ' Length of data in file
If Fl = 0 Then Close DataFile: Exit Sub
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
ReDim Chunk(Fragment)
Chunk() = RS!logo.GetChunk(Fragment)
Put DataFile, , Chunk()
For i = 1 To Chunks
ReDim Buffer(ChunkSize)
Chunk() = RS!logo.GetChunk(ChunkSize)
Put DataFile, , Chunk()
Next i
Close DataFile
FileName = "pictemp"
Picture1.Picture = LoadPicture(FileName)
End Sub
debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
good evening:
how could this code be fitted to the above code (to what i have posted)..
for example, in my form i have imgPhoto in the form, having UPLOAD Picture button, then after locating the photo by this code:
Private Sub cmdUpload_Click()
cdPhoto.Filter = "Image Files (*.jpg;*.gif;*.bmp)|*.jpg;*.gif;*.bmp"
cdPhoto.ShowOpen
filepath = cdPhoto.FileName
imgPhoto.Picture = LoadPicture(filepath)
End Sub
by means of this code, i have successfully loaded the picture within the image on the form but for the SAVE BUTTON?
for database access:
i have field Photo defined as "OLE Object" in MS Access Datafield
what should be on the cmdSave button:
together with this,
Set rs = New ADODB.Recordset
With rs
.Open squery, conn, adOpenStatic, adLockPessimistic
.AddNew
.Fields("EmpID") = txtEmpID.Text
.Fields("LName") = txtLast.Text
.Fields("FName") = txtFirst.Text
.Fields("MName") = txtMiddle.Text
.Fields("Photo")=????
.Update
what should be added with this code or anything much simpler than the one you have given?
thank you very much for the reply.
arvin2006
Junior Poster in Training
69 posts since Sep 2006
Reputation Points: 15
Solved Threads: 4
You need to do the following .
1.add all the records except picture
2.update the recordset
3.edit the same record
4.update the picture field with the photo.
5.update the recordset again.
debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
good morning, i have successfully saved the picture together with the data by this code:
Private Sub cmdSave_Click()
Dim picsm As ADODB.Stream
Set picsm = New ADODB.Stream
picsm.Type = adTypeBinary
picsm.Open
picsm.LoadFromFile filepath
With rs
.AddNew
.Fields("Last") = txtLast.Text
.Fields("First") = txtFirst
.Fields("Pic").Value = picsm.Read
imgPic.Picture = LoadPicture(filepath)
.Update
MsgBox "success"
End With
picsm.Close
Set picsm = Nothing
End Sub
but my problem now is on how to retrieve the picture and load it into the image control on the form:
i have this code but it is working with data only, it retrieves the data but not the image.
Private Sub cmdSearch_Click()
On Error GoTo notfound
Dim photo As String
squery = "select * from tblpic where Last='" & txtLast.Text & "'"
Call ExecuteCommand
photo = rs.Fields("pic").Value
With Me
.txtLast = rs!Last
.txtFirst = rs!First
.imgPic.Picture = LoadPicture(photo)
End With
Exit Sub
notfound:
MsgBox "Not Found"
End Sub
any idea?
thank you...
arvin2006
Junior Poster in Training
69 posts since Sep 2006
Reputation Points: 15
Solved Threads: 4
good morning, i have successfully saved the picture together with the data by this code:
Private Sub cmdSave_Click()
Dim picsm As ADODB.Stream
Set picsm = New ADODB.Stream
picsm.Type = adTypeBinary
picsm.Open
picsm.LoadFromFile filepath
With rs
.AddNew
.Fields("Last") = txtLast.Text
.Fields("First") = txtFirst
.Fields("Pic").Value = picsm.Read
imgPic.Picture = LoadPicture(filepath)
.Update
MsgBox "success"
End With
picsm.Close
Set picsm = Nothing
End Sub
but my problem now is on how to retrieve the picture and load it into the image control on the form:
i have this code but it is working with data only, it retrieves the data but not the image.
Private Sub cmdSearch_Click()
On Error GoTo notfound
Dim photo As String
squery = "select * from tblpic where Last='" & txtLast.Text & "'"
Call ExecuteCommand
photo = rs.Fields("pic").Value
With Me
.txtLast = rs!Last
.txtFirst = rs!First
.imgPic.Picture = LoadPicture(photo)
End With
Exit Sub
notfound:
MsgBox "Not Found"
End Sub
any idea?
thank you...
wow, it was a year ago.. anyway, i was able to manage it..
give me your email and i'll show.. i will find my code...
arvin2006
Junior Poster in Training
69 posts since Sep 2006
Reputation Points: 15
Solved Threads: 4
Amrahim, I see you are a new poster, so welcome. You will have to open a new post though, this is quite old. I'll post all the code needed for save, edit, open and show a picture in vb6/access.
How to open a new thread? Go to the top of the page, hover your mouse over 'Software' (Located between >>>Editorial, Hardware & Software, SOFTWARE, Web Development etc<<<.
Click on VB4/5/6. Once the page opens, at the top as well, it gives you the option to open a new thread. I have for now included the link below. Just click on it and post your question.
Thank you so much.
http://www.daniweb.com/forums/newthread.php?do=newthread&f=4
AndreRet
Senior Poster
3,922 posts since Jan 2008
Reputation Points: 334
Solved Threads: 350
I believe the code he is referring to can be found in this post .
If you have further questions, please start a new thread of your own and post your current code and specific questions.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847