0

I am attempting to retrieve a BLOB value from SQLite3 through VBA. The bytes were entered from a http return using Python. They represent an image(TIF) that will later be returned to the user. I will start by saying that, due to restrictions on the computers being used to perform this action, I will be using VBA/Excel to do so. I have already completed this process through Python and C# to verify there is nothing wrong with the data I am pulling out. Now I need to get it in to VBA to make it easily distributable to the users.

In VBA I am able to get the record values out and iterate over them as needed, but whenever i get to the byte value my Excel Immediate Window gives me the box that says

"EXCEL NOT RESPONDING, RESTART OR DEBUG" etc.

I have done many different things too, not just try to write the bytes to a file. I have tried simply

Dim byt() as bytes
byt = rcdset.Fields("IMAGE_STRING").Value

Also

LenB(rcdset.Fields("IMAGE_STRING").Value)

and even just

Debug.Print rcdset.Fields("IMAGE_STRING").Value

No matter what I am doing, when I step through the code that touches rcdset.Fields("IMAGE_STRING").Value it stops.

Current code/attempt:

Dim stream As New ADODB.stream
OutFile = "P:\Testie.TIF"
stream.Type = adTypeBinary
stream.Open
stream.Write rcdset.Fields("IMAGE_STRING").Value
stream.SaveToFile (OutFile)

And of course, as soon as it gets to stream.Write rcdset.Fields("IMAGE_STRING").Value and calls that value it goes down. I have tried accessing the field all different ways such as rcdset.Fields("IMAGE_STRING"), "rcdset("IMAGE_STRING"), etc.

How can I create this image file? I go write over to python and do a simple

with open(f'''P:\\{name}.{ext}''', 'wb') as of:
    of.write(bytes)

and it creates write away. C# same with of course a little more code but no problem. This is driving me NUTS. Can VBA not handle this kind of data?

Full code below and any help is appreciated!

Dim file As New Scripting.FileSystemObject
Dim conn As New ADODB.Connection
Dim rcdset As New ADODB.Recordset
Dim stream As New ADODB.stream
'Dim rec As New ADODB.Field
Dim filepath As String
Dim bytes As Byte
Dim connStr As String
Dim sql As String
Dim lastrow As Integer
Dim ScacRange As Range
Dim RowCounter As Integer
Dim Pro As String
Dim Carrier As String
Dim PaperType As String
Dim downloaded() As String
Dim i As Integer
Dim paperTitle As String
Dim fileLen As Long
Dim OutFile As String
Dim binlength As Long

lastrow = Cells(Rows.Count, 1).End(xlUp).Row
If lastrow > 501 Then
    MsgBox "You can only process 500 at one time.", vbInformation
    Exit Sub
End If

Set ScacRange = Range(Cells(2, 2), Cells(lastrow, 2))

filepath = Cells(1, 5).Value


RowCounter = 2
For Each Scac In ScacRange
    If Len(Trim(Scac.Value)) <> 4 Then
        MsgBox "Row " & CStr(RowCounter) & " does not contain a 4 letter Scac.  Please correct and try again."
        Exit Sub
    End If
    RowCounter = RowCounter + 1
Next Scac

If Not file.FolderExists(filepath) Then
    MsgBox "The folder '" & filepath & "' does not exist.  Please enter a valid folder.", vbExclamation, "File Path Error"
    Exit Sub
End If

connStr = "DRIVER=SQLite3 ODBC Driver;Database=Z:\IMAGE.db"


On Error GoTo Cleanup

    conn.Open connStr

    For Each Scac In ScacRange

        If IsEmpty(Scac) Or IsEmpty(Scac.Offset(0, -1)) Or IsEmpty(Scac.Offset(0, 1)) Then
            Scac.Offset(0, 2).Value = "Not all values present. No image retreived"
            GoTo NextIter
        End If

        Pro = Trim(Scac.Offset(0, -1).Value)
        Carrier = Trim(UCase(Scac.Value))
        PaperType = Scac.Offset(0, 1).Value

        If PaperType = "BOTH" Then
            sql = "SELECT IMAGE_ID, IMAGE_STRING, IMAGE_FILE_EXT FROM DOCUMENTS WHERE PRO_NUMBER = '" & Pro & "' AND SCAC = '" & Carrier & "' AND IMAGE_TYPE IN('BL','DR')"
            rcdset.Open sql, conn
            If rcdset.BOF And rcdset.EOF Then
                Scac.Offset(0, 2).Value = "No Images available"
                rcdset.Close
                GoTo NextIter
            End If
            rcdset.MoveFirst
            i = 0
            Do Until rcdset.EOF
                paperTitle = rcdset.Fields(0).Value
                ReDim Preserve downloaded(i)
                downloaded(i) = paperTitle
                OutFile = "P:\Testie.TIF"
                stream.Type = adTypeBinary
                stream.Open
                ''''''''''''The below line is where it breaks down''''''''''''''''''''''''''''
                stream.Write rcdset.Fields("IMAGE_STRING").Value
                stream.SaveToFile OutFile, adSaveCreateOverWrite

                'do work
                i = i + 1
                Debug.Print paperTitle
                rcdset.MoveNext
            Loop
            rcdset.Close
            Scac.Offset(0, 2).Value = Join(downloaded, ", ") & " downloaded to folder."
            ReDim downloaded(0)
        Else:
            sql = "SELECT IMAGE_ID, IMAGE_STRING FROM DOCUMENTS WHERE PRO_NUMBER = '" & Pro & "' AND SCAC = '" & Carrier & "' AND IMAGE_TYPE = '" & PaperType & "'"
            rcdset.Open sql, conn
            If rcdset.BOF And rcdset.EOF Then
                Scac.Offset(0, 2).Value = "No Images available"
                rcdset.Close
                GoTo NextIter
            End If
            rcdset.MoveFirst
            Debug.Print rcdset.Fields(0).Value
            'dow work
            Scac.Offset(0, 2).Value = "Downloaded to folder"
            rcdset.Close
        End If
NextIter:
    Next Scac

Cleanup:

If IsObject(rcdset) Then
    If rcdset.State = 1 Then
        rcdset.Close
    End If
    Set rcdset = Nothing
End If

If IsObject(conn) Then
    If conn.State = 1 Then
        conn.Close
    End If
    Set conn = Nothing
End If

If IsObject(file) Then
    Set file = Nothing
End If

Error picture below

https://www.google.com/imgres?imgurl=https%3A%2F%2Fwww.stellarinfo.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F02%2FExcel-not-responding.png&imgrefurl=https%3A%2F%2Fwww.stellarinfo.com%2Fblog%2Ffix-microsoft-excel-is-not-responding-error%2F&docid=XoYYxJE6mc5PtM&tbnid=coD0C_Y_q1La5M%3A&vet=10ahUKEwiM-_2Yx6fgAhUptIMKHepGDLYQMwhBKAEwAQ..i&w=363&h=266&client=firefox-b-1-d&bih=944&biw=1920&q=excel%20not%20responding%20error&ved=0ahUKEwiM-_2Yx6fgAhUptIMKHepGDLYQMwhBKAEwAQ&iact=mrc&uact=8

I got this picture from google. Couldnt get my image embedded

  • Are you using proper [error handling](https://stackoverflow.com/q/6028288/1422451)? Do so to catch runtime exceptions. Even try catching [ADODB errors](https://stackoverflow.com/a/28618190/1422451). Report back actual error messages. – Parfait Feb 05 '19 at 20:55
  • @Parfait. yes I have the code wrapped in a "On Error GoTo" block and it still just shuts down. The only error i can see is to click the Debug option before it closes and open debugging in Visual Studio 2017 and it shows this error. "Unhandled exception at 0x752A9B60 (msvcrt.dll) in EXCEL.EXE: 0xC0000005: Access violation reading location 0x08EF0000." I will repost an edit with full. I removed some stuff that i thought was unimportant. Thanks – PMCallahan Feb 05 '19 at 22:14
  • Your *Cleanup* handler does not report any error number or message: `MsgBox Err.Number & " - " & Err.Description` (see links in my comment). In fact your *Cleanup* should be the exit handler as it cleans out `set` objects. The error handler should report message and then call exit handler: `Resume Cleanup`. – Parfait Feb 06 '19 at 15:07
  • Also, please edit with your environment. What Windows version? What version of MS Office? Is it 32 or 64-bit? What version of SQLite? Is it 32 or 64-bit? Same with ODBC driver? Search and open "odbc". Ideally all architectures should align. – Parfait Feb 06 '19 at 15:29
  • @Parfait. I understand the error handling is not correct. The point is that it won't even jump to that section of the code though. It just dies. Ill edit with a picture to show you. Office 2016, Windows 7 64 bit, SQLite3 vers. 3.21.0. ODBC 64bit – PMCallahan Feb 06 '19 at 16:39
  • How large are these images? How large is the SQLite database? You may need to wait for streaming to end before moving to next row. Try retrieving only **one** image outside of *any* looping (in smaller test subroutine). – Parfait Feb 06 '19 at 18:00
  • @Parfait. This is one of 2 records in the DB. The image is 388kb. I tried one in a separate sub and still excel closed on me. Sadface – PMCallahan Feb 06 '19 at 22:35

0 Answers0