是的,Dim Img As Attachment看起来很诱人,但是Attachment(实际上是Access.Attachment)是指可以在表单上使用的 Attachment 控件(就像Access.TextBox)并且似乎不适合您的预期目的。
用于存储这种二进制数据的唯一本地 VBA 类型是 Byte 值数组,但通常在处理字节数组时,我们最终会循环遍历并逐字节处理它们,这是乏味且低效的。
您可能会考虑使用二进制ADODB.Stream对象作为“变量”。您可以创建一个函数来检索附件字节并像这样在 Stream 中返回它们
Option Compare Database
Option Explicit
Public Function GetLogoAsStream() As ADODB.Stream
Dim cdb As DAO.Database, rstMain As DAO.Recordset, rstAttach As DAO.Recordset2, fldAttach As DAO.Field2
' Project references required for early binding:
' Windows Script Host Object Model
' Microsoft ActiveX Data Objects 2.8 Library
Dim fso As FileSystemObject, tempFileSpec As String
Static strm As ADODB.Stream
If strm Is Nothing Then
Set fso = New FileSystemObject
tempFileSpec = fso.GetSpecialFolder(TemporaryFolder) & "\" & fso.GetTempName
Set fso = Nothing
Set cdb = CurrentDb
Set rstMain = cdb.OpenRecordset( _
"SELECT [AttachmentFiles] " & _
"FROM [AttachmentsTable] " & _
"WHERE [Description]='SO logo'", _
dbOpenSnapshot)
Set rstAttach = rstMain("AttachmentFiles").Value
' make sure we use the correct file extension
tempFileSpec = tempFileSpec & "." & rstAttach.Fields("FileType").Value
Set fldAttach = rstAttach.Fields("FileData")
fldAttach.SaveToFile tempFileSpec
Set fldAttach = Nothing
rstAttach.Close
Set rstAttach = Nothing
rstMain.Close
Set rstMain = Nothing
Set cdb = Nothing
Set strm = New ADODB.Stream
strm.Type = adTypeBinary
strm.Open
strm.LoadFromFile tempFileSpec
Kill tempFileSpec
End If
strm.Position = 0
Set GetLogoAsStream = strm
End Function
然后如果你有一个像这样的带有空图像控件的报告

和一个On Load像这样的事件过程来从你的“变量”加载图像控件.PictureData(实际上是一个返回 ADODB.Stream 的函数)
Private Sub Report_Load()
Me.LogoImage.PictureData = GetLogoAsStream.Read
End Sub
它可以产生这样的东西
