| | |
VB Add/Edit/Save Pictures in Access DB
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
please try to use the sample code to store picture.
this sample code uses ADO for database connection
this sample code uses ADO for database connection
vb Syntax (Toggle Plain Text)
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
Share your Knowledge.
please try to use the sample code to retrieve picture from database tables.
again this sample code uses ADO for database connection.
again this sample code uses ADO for database connection.
vb Syntax (Toggle Plain Text)
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
Share your Knowledge.
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.
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.
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...
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...
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Minimize a form in its own window Like Photoshop
- Next Thread: Limiting Cursor VB6
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






