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

后端 未结 1 1016
鱼传尺愫
鱼传尺愫 2020-12-18 16:44

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 Workbenc
1条回答
  •  隐瞒了意图╮
    2020-12-18 17:17

    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
    

    0 讨论(0)
提交回复
热议问题