How to load picture into MS-Access Image control from ADO recordset field? - image

Using:
MS-Access 2013 SP1 x86
MS-SQL Server 2012 SP1 CU8 x64
Connection via ODBC DSN, SQL Server driver
UpScene Database Workbench Pro v4.4.4 Pro for MS-SQL Server
My Access 2013 application uses SQL Server 2012 as the backend database with ODBC. I'm using VBA/ADO to read/write data to the database.
I have been unsuccessful so far in retrieving an image from the database and assigning it to an image control on an Access form. The image is stored in a SQL Server table (as a VARBINARY(MAX) field.
At the point where I'm assigning the field to the Image control, it gives a runtime error: "Type mismatch". The image stored in the database as a Bitmap image. I tried with Jpeg earlier, but it was the same error. How can this be resolved?
SQL Server table definition and stored procedure definition:
CREATE TABLE dbo.tbPhoto (
row_id Int IDENTITY NOT NULL,
student_id Int NOT NULL,
picture VarBinary(max),
date_updated DateTime NOT NULL,
date_created DateTime NOT NULL,
CONSTRAINT PK_tbPhoto PRIMARY KEY CLUSTERED (
row_id
)
)
Access procedures to retrieve the picture:
Private Sub load_studentdetails(AStudentID As Integer)
On Error GoTo errhnd
objStudent.GetStudentDetails AStudentID, StudentDetailsRS
Set Me.fmStudentReg_DtlA.Form.Recordset = StudentDetailsRS
' Me.fmStudentReg_DtlA.Form.Requery
objStudent.GetStudentPicture AStudentID, Me.fmStudentReg_DtlA!imgStudentPic
Exit Sub
errhnd:
MsgBox "Error: " & Err.Description
End Sub
Public Sub GetStudentPicture(AStudentID As Integer, ByRef APicture As Image)
On Error GoTo errhnd
Dim rs As New ADODB.Recordset
Set cmd.ActiveConnection = GetDBConnection
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "dbo.StudentPicture_S"
cmd.Parameters.Refresh
cmd(1) = AStudentID
rs.CursorLocation = adUseClient
rs.Open cmd
Set APicture = rs("picture") '<-----Raises the error: "Type mismatch"
Exit Sub
errhnd:
MsgBox "Error: " & Err.Description
End Sub

Since you are already using an ADODB.Recordset I would suggest that you use an ADODB.Stream object as an intermediary. I just tried the following code and it worked for me:
Option Compare Database
Option Explicit
Private Sub cmdGetPhoto_Click()
Dim cdb As DAO.Database
Dim con As ADODB.Connection, rst As ADODB.Recordset, stm As ADODB.Stream
Set cdb = CurrentDb
Set con = New ADODB.Connection
con.Open Mid(cdb.TableDefs("dbo_Clients").Connect, 6)
Set rst = New ADODB.Recordset
rst.Open "SELECT Photo FROM Clients WHERE ClientID=1", con, adOpenStatic, adLockOptimistic
Set stm = New ADODB.Stream
stm.Type = adTypeBinary
stm.Open
stm.Write rst("Photo").Value ' write bytes to stream
stm.Position = 0
Me.Image0.PictureData = stm.Read ' load bytes into Image control on form
stm.Close
Set stm = Nothing
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
Set cdb = Nothing
End Sub

Related

Not returning any result while retrieving data from oracle database using vb6, please help me to locate the error

When i check the result set. record count it returns -1 and while checking the recordset.EOF it returns true, thus the result set does not contain any value.
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strSQL
Private Sub cmd_login_Click()
Dim pass As String
con.ConnectionString = "Provider=msdaora;Data Source=localhost;User Id=ams;Password=krishnan;"
con.Open
strSQL = "Select passwrd from ams.login_details where username = 'Admin'"
rs.Open strSQL, con
If Not (rs.EOF) Then
If rs("passwrd") = txt_pass.Text Then
MsgBox rs("passwrd")
End If
End If
rs.Close
con.Close
End Sub
I forget to commit the statements in Oracle Sql Developer that's why not data was fetched from the database, When i executed the commit statement, it's working fine.

Excel is being closed while connecting to Oracle using VBA

I am trying to connect to Oracle using Excel VBA but when the code is run, Excel is closed and reopened at line MsgBox rs.recordCount
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim query As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo final1
query = "Select * from EMP where EMP_NO='998234'"
strcon = "Driver={Oracle in OraClient11g_home1};Dbq=smqa;Uid=MyUserID;Pwd=MyPassword;"
con.Open (strcon)
rs.CursorType = 1
rs.Open query, con
rs.MoveLast
MsgBox rs.recordCount
rs.Close
Set s = Nothing
con.Close
Set con = Nothing
final1:
MsgBox err.Number & " " & err.Description
End Sub
I am running this code on Windows 7 and trying to connect Oracle 11g. Do I need any additional setup to connect Oracle Db using Excel VBA or is something wrong in the code?

How to connect a database to crystal report at run time?

I am developing winform application in vb6. I am using crystal report 4.6. I have created a crystal report which shows all data from a table (MS Access). And I unchecked save data with report and i saved the report. I just want to invoke it in application. So I included the component CrystalReportControl in my application. Now i want to set the records to be displayed in the report. The records are selected according to the user input to the text box.
Records are retrived from the database is done in following code.
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Command1_Click()
Set conn = New ADODB.Connection
conn.Open "provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path &"\faculty.mdb"
Set rs = New ADODB.Recordset
rs.Open "select * from facultydetails where eid=1234", conn, adOpenDynamic, adLockPessimistic
CrystalReport1.ReportFileName = App.Path & "\faculty.rpt"
Set CrystalReport1.DataSource = rs
CrystalReport1.Action = 1
End Sub
Gives an error for the line: Set CrystalReport1.DataSource = rs :
as Property is write-only.
Tell me how the records of the report can be dynamic? Plz help me...
Instead of
Set CrystalReport1.DataSource = rs
do
CrystalReport1.DataSource = rs
EDIT:
Take a look at the following example and see if that will help you:
'CrystalReport1 is the name of the dsr file
Dim Report As New CrystalReport1
Dim cdoRowset As CrystalDataObject.CrystalComObject
Dim varArray() As Variant
'Open ADO Connection
Set m_cnAdo = New ADODB.Connection
m_cnAdo.ConnectionString = "DRIVER={SQL Server};UID=[UserID];PWD=[Password]" _
& ";SERVER=[Server];DATABASE=[Database]"
m_cnAdo.Open
Dim rsAdo As ADODB.Recordset
Dim cmdAdo As ADODB.Command
'Using Embedded Query
Set cmdAdo = New ADODB.Command
Set rsAdo = New ADODB.Recordset
cmdAdo.ActiveConnection = m_cnAdo
cmdAdo.CommandText = "SELECT * FROM Table WHERE Param = " & lngParam1
cmdAdo.CommandType = adCmdText
Set rsAdo = cmdAdo.Execute
Report.Database.SetDataSource rsAdo, 3, 1

Multiple-step OLEDB Error for SQLSERVER.CE.OLEDB.3.5 Connection

I am trying to connect to a SQL Server Compact Edition .sdf file with following connection string;
connMRC.ConnectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=" & App.Path & "\Freeweigh.sdf;"
I get the following error everytime I try running an SQL command or opening a recordset:
Multiple-step OLE DB generated errors. Check each OLE DB status value, if available. No work was done.
I am using VB 6.0 and SQL Server Compact 3.5 SP2
Here's the code:
Public Sub opnConnectionC()
'Code for opening the ADO Connection
chkConn = connMRC.State
If chkConn = adStateClosed Then
connMRC.ConnectionString = "Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;Data Source=" & App.Path & "\Freeweigh.sdf;"
connMRC.Open
End If
End Sub
Public Sub opnRecordsetC(rsOpen As Recordset)
'Code for opening the ADO Recordset
chkRs = rsOpen.State
If chkRs = adStateClosed Then
rsOpen.Source = strSQLC
rsOpen.CursorType = adOpenDynamic
rsOpen.LockType = adLockOptimistic
rsOpen.ActiveConnection = connMRC
rsOpen.Open
End If
End Sub
Private Sub tmrUpload_Timer()
Dim cmdUpload As New ADODB.Command
Dim rsFetch As New ADODB.Recordset
Call opnConnectionC
strSQLC = "SELECT Product FROM VehicleWeights"
Call opnRecordsetC(rsFetch)
rsFetch.MoveFirst
MsgBox (rsFetch.Fields("Product").Value)
Call clsConnectionC
End Sub
You can only open a forward only, read only recordset

VB6 and ODBC commands

I started a thread here, this is a continuation from that post: Calling a Module and a Class within a Form
I can't find the information I need to successfully query our database. What I need to do is to get a single value from a Pervasive database. I cannot find the list of ODBC commands to do this with.
Can someone please point me to some documentation that deals with these Pervasive ODBC commands? I'm using ADO ODBC for the connection.
EDIT:
I am also attempt to connect to a MySQL database as well and am hitting the same error. Here is a test Sub I created to call my MySQL function. The error is the same for MySQL as it is for Pervasive: "Object variable or With Block variable not set"
Public Sub testMe(id)
Dim MySqlConn As adodb.Connection 'Do I need this here or in the MySQL function?
Set MySqlConn = ConnectMySQL()
MySqlConn.Open "SELECT * FROM test", MySqlConn, adOpenDynamic, adLockOptimistic
End Function
First, you need to connect to the database. According to this website the connection string would be in this format:
Driver={Pervasive ODBC Client
Interface};ServerName=myServerAddress;dbq=#dbname;
Starting with code from your previous post, it could be extended like this:
Option Explicit
Public Function getEmployee() As String
Dim MyConnection As ADODB.Connection
Dim CM As ADODB.Command
Dim RS As ADODB.Recordset
Set MyConnection = ConnectSQL()
'one way using command objects
Set CM = New ADODB.Command
Set CM.ActiveConnection = MyConnection
CM.CommandType = adCmdText
CM.CommandText = "select * from <table>"
Set RS = New ADODB.Recordset
RS.Open CM, , adOpenStatic, adLockBatchOptimistic
'another way using just the connection
Set RS = MyConnection.Execute("select * from <table>")
'return the data
getEmployee = RS.Fields(0).Value
End Function
Public Function ConnectSQL() As ADODB.Connection
Set ConnectSQL = New ADODB.Connection
ConnectSQL.Open "Driver={MySQL ODBC Client Interface};ServerName=localhost;dbq=#testdb"
End Function

Resources