سؤال

Using:

  1. MS-Access 2013 SP1 x86
  2. MS-SQL Server 2012 SP1 CU8 x64
  3. Connection via ODBC DSN, SQL Server driver
  4. 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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top