【问题标题】:How to load picture into MS-Access Image control from ADO recordset field?如何从 ADO 记录集字段将图片加载到 MS-Access Image 控件中?
【发布时间】:2014-01-29 21:53:58
【问题描述】:

使用:

  1. MS-Access 2013 SP1 x86
  2. MS-SQL Server 2012 SP1 CU8 x64
  3. 通过 ODBC DSN、SQL Server 驱动程序连接
  4. UpScene Database Workbench Pro v4.4.4 Pro for MS-SQL Server

My Access 2013 应用程序使用 SQL Server 2012 作为带有 ODBC 的后端数据库。我正在使用 VBA/ADO 向数据库读取/写入数据。

到目前为止,我一直未能从数据库中检索图像并将其分配给 Access 表单上的图像控件。图像存储在 SQL Server 表中(作为 VARBINARY(MAX) 字段。

在我将字段分配给 Image 控件时,它给出了运行时错误:“类型不匹配”。存储在数据库中的图像作为位图图像。我之前尝试过使用 Jpeg,但这是同样的错误。如何解决?

SQL Server 表定义和存储过程定义:

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
    )
)

获取图片的访问程序:

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

【问题讨论】:

    标签: image ms-access ado recordset


    【解决方案1】:

    由于您已经在使用 ADODB.Recordset,我建议您使用 ADODB.Stream 对象作为中介。我刚刚尝试了以下代码,它对我有用:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      相关资源
      最近更新 更多