The error is somewhat self-explanatory: an image control in Microsoft Access can be either bound to a file location, or can be set to a .dib
image (device independent bitmap format, one of the more obscure image formats).
Working around it, though, is not that easy.
You can work around that limitation in several ways:
- Use an ActiveX control that supports multiple image formats (there are multiple to be found)
- Save the image to disk in a temporary folder, and set the images control source to its location
- Use a web browser control, and use the HTML
<img />
tag to display your image, using an embedded BASE64 image
Here is the example code for approach 3:
First, we need to be able to convert the binary code contained in the OLE object to BASE64:
Public Function ToBase64(Bytes As Variant) As String
Dim XMLElement As Object
Set XMLElement = CreateObject("Msxml2.DOMDocument.6.0").createElement("tmp")
XMLElement.DataType = "bin.base64"
XMLElement.nodeTypedValue = Bytes
ToBase64 = Replace(XMLElement.Text, vbLf, "")
End Function
Then, we can use a web browser control, and insert a web page with the BASE64-encoded image into it:
Public Sub InsertImageInControl()
Dim wb As Object
Set wb = MyWebbrowserControl.Object
With wb
.Navigate2 "about:blank"
Do Until .ReadyState = 4 '=READYSTATE_COMPLETE
'This is a somewhat inefficient way to wait, but loading a blank page should only take a couple of milliseconds
DoEvents
Loop
.Document.Open
.Document.Write "<!DOCTYPE html><HTML><HEAD><TITLE>A</TITLE></HEAD><BODY scroll=""no"" style=""margin: 0px; padding: 0px;"">"
.Document.Write "<img alt="""" style=""width:100%; margin: 0px; padding: 0px;"" src=""data:image/jpg;base64,"
.Document.Write ToBase64(MyOLEObject.Value)
.Document.Write """ />"
.Document.Write "</BODY></HTML>"
.Document.Close
End With
End Sub
Where MyWebbrowserControl
is the name of your webbrowser control, image/jpg
is your image type, and MyOLEObject
is your OLE object.
Tips:
- Don't use the WebBrowser ActiveX control, but use the one that comes with Access. Else, you will get an outdated version of Internet Explorer with a 3d border that can't be removed.
- Set the control source for the web browser control to
="about:blank"
to initialize it as a blank page
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…