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: