Image BLOBs Please Help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mikael
    New Member
    • Nov 2006
    • 16

    Image BLOBs Please Help!

    I am using Access 2003. I would like to use BLOB to display an image on a form. I have referred to the kb article: http://support.microsoft.com/default...EN-US;Q103257&

    But the code is not compiling. Could someone give me some help? I haven't found any references for helping with BLOB.
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by Mikael
    I am using Access 2003. I would like to use BLOB to display an image on a form. I have referred to the kb article: http://support.microso ft.com/default.aspx?sc id=KB;EN-US;Q103257&

    But the code is not compiling. Could someone give me some help? I haven't found any references for helping with BLOB.
    Hi Mikael

    I know we have a couple of experts with knowledge on this I'm just posting here to move this back to the top of the list.

    Comment

    • PEB
      Recognized Expert Top Contributor
      • Aug 2006
      • 1418

      #3
      Where was the error?

      Code:
      '**************************************************************
            ' FUNCTION: ReadBLOB()
            '
            ' PURPOSE:
            '   Reads a BLOB from a disk file and stores the contents in the
            '   specified table and field.
            '
            ' PREREQUISITES:
            '   The specified table with the OLE object field to contain the
            '   binary data must be opened in Visual Basic code (Access Basic
            '   code in Microsoft Access 2.0 and earlier) and the correct record
            '   navigated to prior to calling the ReadBLOB() function.
            '
            ' ARGUMENTS:
            '   Source - The path and filename of the binary information
            '            to be read and stored.
            '   T      - The table object to store the data in.
            '   Field  - The OLE object field in table T to store the data in.
            '
            ' RETURN:
            '   The number of bytes read from the Source file.
            '**************************************************************
            Function ReadBLOB(Source As String, T As Recordset, _
            sField As String)
                Dim NumBlocks As Integer, SourceFile As Integer, i As Integer
                Dim FileLength As Long, LeftOver As Long
                Dim FileData As String
                Dim RetVal As Variant
      
                On Error GoTo Err_ReadBLOB
      
                ' Open the source file.
                SourceFile = FreeFile
                Open Source For Binary Access Read As SourceFile
      
                ' Get the length of the file.
                FileLength = LOF(SourceFile)
                If FileLength = 0 Then
                    ReadBLOB = 0
                    Exit Function
                End If
      
                ' Calculate the number of blocks to read and leftover bytes.
                NumBlocks = FileLength \ BlockSize
                LeftOver = FileLength Mod BlockSize
      
                ' SysCmd is used to manipulate status bar meter.
                RetVal = SysCmd(acSysCmdInitMeter, "Reading BLOB", _
                         FileLength \ 1000)
      
                ' Put the record in edit mode.
                T.Edit
      
                ' Read the leftover data, writing it to the table.
                FileData = String$(LeftOver, 32)
                Get SourceFile, , FileData
                T(sField).AppendChunk (FileData)
      
                RetVal = SysCmd(acSysCmdUpdateMeter, LeftOver / 1000)
      
                ' Read the remaining blocks of data, writing them to the table.
                FileData = String$(BlockSize, 32)
                For i = 1 To NumBlocks
                    Get SourceFile, , FileData
                    T(sField).AppendChunk (FileData)
      
                    RetVal = SysCmd(acSysCmdUpdateMeter, BlockSize * i / 1000)
                Next i
      
                ' Update the record and terminate function.
                T.Update
                RetVal = SysCmd(acSysCmdRemoveMeter)
                Close SourceFile
                ReadBLOB = FileLength
                Exit Function
      
            Err_ReadBLOB:
                ReadBLOB = -Err
                Exit Function
      
            End Function
      
            '**************************************************************
            ' FUNCTION: WriteBLOB()
            '
            ' PURPOSE:
            '   Writes BLOB information stored in the specified table and field
            '   to the specified disk file.
            '
            ' PREREQUISITES:
            '   The specified table with the OLE object field containing the
            '   binary data must be opened in Visual Basic code (Access Basic
            '   code in Microsoft Access 2.0 or earlier) and the correct
            '   record navigated to prior to calling the WriteBLOB() function.
            '
            ' ARGUMENTS:
            '   T           - The table object containing the binary information.
            '   sField      - The OLE object field in table T containing the
            '                 binary information to write.
            '   Destination - The path and filename to write the binary
            '                 information to.
            '
            ' RETURN:
            '   The number of bytes written to the destination file.
            '**************************************************************
            Function WriteBLOB(T As Recordset, sField As String, _
            Destination As String)
                Dim NumBlocks As Integer, DestFile As Integer, i As Integer
                Dim FileLength As Long, LeftOver As Long
                Dim FileData As String
                Dim RetVal As Variant
      
                On Error GoTo Err_WriteBLOB
      
                ' Get the size of the field.
                FileLength = T(sField).FieldSize()
                If FileLength = 0 Then
                    WriteBLOB = 0
                    Exit Function
                End If
      
                ' Calculate number of blocks to write and leftover bytes.
                NumBlocks = FileLength \ BlockSize
                LeftOver = FileLength Mod BlockSize
      
                ' Remove any existing destination file.
                DestFile = FreeFile
                Open Destination For Output As DestFile
                Close DestFile
      
                ' Open the destination file.
                Open Destination For Binary As DestFile
      
                ' SysCmd is used to manipulate the status bar meter.
                RetVal = SysCmd(acSysCmdInitMeter, _
                "Writing BLOB", FileLength / 1000)
      
                ' Write the leftover data to the output file.
                FileData = T(sField).GetChunk(0, LeftOver)
                Put DestFile, , FileData
      
                ' Update the status bar meter.
                RetVal = SysCmd(acSysCmdUpdateMeter, LeftOver / 1000)
      
                ' Write the remaining blocks of data to the output file.
                For i = 1 To NumBlocks
                    ' Reads a chunk and writes it to output file.
                    FileData = T(sField).GetChunk((i - 1) * BlockSize _
                       + LeftOver, BlockSize)
                    Put DestFile, , FileData
      
                    RetVal = SysCmd(acSysCmdUpdateMeter, _
                    ((i - 1) * BlockSize + LeftOver) / 1000)
                Next i
      
                ' Terminates function
                RetVal = SysCmd(acSysCmdRemoveMeter)
                Close DestFile
                WriteBLOB = FileLength
                Exit Function
      
            Err_WriteBLOB:
                WriteBLOB = -Err
                Exit Function
      
            End Function
      
            '**************************************************************
            ' SUB: CopyFile
            '
            ' PURPOSE:
            '   Demonstrates how to use ReadBLOB() and WriteBLOB().
            '
            ' PREREQUISITES:
            '   A table called BLOB that contains an OLE Object field called
            '   Blob.
            '
            ' ARGUMENTS:
            '   Source - The path and filename of the information to copy.
            '   Destination - The path and filename of the file to write
            '                 the binary information to.
            '
            ' EXAMPLE:
            '   CopyFile "c:\windows\winfile.hlp", "c:\windows\winfil_1.hlp"
            '**************************************************************
            Sub CopyFile(Source As String, Destination As String)
                Dim BytesRead As Variant, BytesWritten As Variant
                Dim Msg As String
                Dim db As Database
                Dim T As Recordset
      
                ' Open the BLOB table.
                Set db = CurrentDb()
                Set T = db.OpenRecordset("BLOB", dbOpenTable)
      
                ' Create a new record and move to it.
                T.AddNew
                T.Update
                T.MoveLast
      
                BytesRead = ReadBLOB(Source, T, "Blob")
      
                Msg = "Finished reading """ & Source & """"
                Msg = Msg & Chr$(13) & ".. " & BytesRead & " bytes read."
                MsgBox Msg, 64, "Copy File"
      
                BytesWritten = WriteBLOB(T, "Blob", Destination)
      
                Msg = "Finished writing """ & Destination & """"
                Msg = Msg & Chr$(13) & ".. " & BytesWritten & " bytes written."
                MsgBox Msg, 64, "Copy File"
            End Sub

      Comment

      • Mikael
        New Member
        • Nov 2006
        • 16

        #4
        The error is this line:

        Set T = db.OpenRecordse t("BLOB", dbOpenTable)

        I tried using the constants on top, and replacing dbOpenTable with what its supposed to actual be, but still no luck.

        Comment

        • PEB
          Recognized Expert Top Contributor
          • Aug 2006
          • 1418

          #5
          What is the name of the table that you should create for the blobs?
          If different than Blob you need to change there!

          Comment

          • Mikael
            New Member
            • Nov 2006
            • 16

            #6
            I just recreated the incident. I followed the article exactly, and then when I type the:

            CopyFile "c:\windows\win file.hlp", "c:\windows\win fil_1.hlp"

            I get an error (type mismatch) on this line:

            Set T = db.OpenRecordse t("BLOB", dbOpenTable)

            Is Access 2003, 2.0? I tried uncommenting the lines the initializations at the top, incase it is, but the same error occurs. My table is named BLOB, my module is named BLOB, and i took out all the underscores. Not sure whats wrong.

            Comment

            • PEB
              Recognized Expert Top Contributor
              • Aug 2006
              • 1418

              #7
              Ok in this line change: "BLOB"
              to
              "SELECT * FROM BLOB;"

              Comment

              • PEB
                Recognized Expert Top Contributor
                • Aug 2006
                • 1418

                #8
                Remove DBOPENTABLE!

                Comment

                • Mikael
                  New Member
                  • Nov 2006
                  • 16

                  #9
                  Originally posted by PEB
                  Remove DBOPENTABLE!
                  What do you mean here? Remove it from where, the constants above?

                  Comment

                  • PEB
                    Recognized Expert Top Contributor
                    • Aug 2006
                    • 1418

                    #10
                    Test the line like this:

                    Set T = db.OpenRecordse t("SELECT * FROM BLOB;")

                    Comment

                    • Mikael
                      New Member
                      • Nov 2006
                      • 16

                      #11
                      Error, Type Mismatch, same line.

                      Comment

                      • PEB
                        Recognized Expert Top Contributor
                        • Aug 2006
                        • 1418

                        #12
                        Ok I will test this code on my computer :) In few minutes i'll tell you what are the results

                        Comment

                        • Mikael
                          New Member
                          • Nov 2006
                          • 16

                          #13
                          Thank you so much, I have gotten no help on this subject for weeks!

                          Comment

                          • PEB
                            Recognized Expert Top Contributor
                            • Aug 2006
                            • 1418

                            #14
                            Interesting in my case there isn't problems....

                            Have you compiled your modules before running the sub?

                            This is from Debug - > Compile

                            Comment

                            • Mikael
                              New Member
                              • Nov 2006
                              • 16

                              #15
                              Never tried. I changed the code back to the original article, and hit debug -> compile test. Now I get a method or data member not found compile error on this line:

                              T.Edit

                              Comment

                              Working...