Access VBA: how to dump OLE field content into bytes

拟墨画扇 提交于 2019-12-25 03:12:11

问题


I have a table containing OLE fields and I would like to see the content of some fields as bytes. I only can view it in the Watch window with BinData array so far . I tried to convert it to a byte array using CByte function but it gives an "Type mismatch" error.

Is there any way to obtain the OLE field content as a byte array?

My code which reads byte by byte field content into Bindata array:

Sub DumpField()

Dim BinData(100) As Variant
Dim r As ADODB.Recordset
Dim i As Integer

Set r = New ADODB.Recordset
With r
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open "SELECT [fBinary] FROM tbDesc WHERE ID=100", CurrentProject.Connection
If .BOF Then MsgBox "No Records"
Exit Sub
End If

For i = 0 To r.Fields(0).ActualSize - 1
BinData(i) = r.Fields(0).GetChunk(1)
Next i

End With
End Sub

回答1:


The OLE field content is a byte array. Using CByte, however, attempts to convert something to a single byte, and fails.

You can print individual bytes in the following way:

Debug.Print r.Fields(0).Value(0) 'First byte

Or store a byte array in one of the following ways:

1: just use a variant

Dim BinData As Variant
BinData = r.Fields(0).Value

2: create a byte array of the appropriate size, then set the byte array to the OLE objects value

Dim BinData() As Byte
Dim objSize As Long
objSize = UBound(r.Fields(0).Value)
Redim BinData(0 To objSize)
BinData = r.Fields(0).Value


来源:https://stackoverflow.com/questions/48842719/access-vba-how-to-dump-ole-field-content-into-bytes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!