Access VBA: how to dump OLE field content into byt

2019-08-23 09:17发布

问题:

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