Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LONG BINARY DATA HELP???

Status
Not open for further replies.

twinlizards

Programmer
May 5, 2003
39
US
Ok, I am having a problem with the "long binary data". Can anyone help me and explain to me what it is and how it gets into an access database? The program I inhereted has there image file saved under this type. Are these files in a dll? If so, then how do I create a dll for my database to get info from?
 
I don't know how your image is stored - "long binary data" means nothing to me, except for the "binary" part.

The binary part means that your image is NOT stored in ASCII/text format, which is true of 100% of image types available. In short, your "long binary data" could be anything at all.


Are you getting this image file in an actual file? If so, try and open the file with a standard image viewer or use the linux FILE command if you have that resource available to you...


I'd recommend you try and find more documentation on your "inherited" program - and do provide more details if your you need to repost this question.

--
Find common answers using Google Groups:

Corrupt MDBs FAQ
 
When I try and use the object or load object it gives me a whole bunch of ways to insert all sorts of image files but usually they r stored as packages and or bitmaps or whatever, but I see the other image files in the previous columns say "long binary data" and I cannot open these. When I use a program that is associated with this database, I can see the image files from the other field but my field that I load in usually I get a runtime error. invalid picture(?). Makes no sens since when I leave a field blank it auto inserts a JPEg image, stored in the folder as notfound. I put a JPEG image in but it usually says package. Also, I can edit paskages and such but the "binary" fields I cannot. Are these files stored in a dll? and then accessed when program used? At this point I am trying to find out how this binary data is inserted or what it is?
 
Nevermind, I got it all from this thread: thread181-544816


Sorry, I don't know anything about embedded-OLE objects and how to add/delete/extract them from the database. I'd recommend you look at the source to your "inherited" system and see exactly how it works with the image data--that's where your answers will come from. The most important thing is to get a firm grasp as to what's going on with the image data already--how it gets into the database, how it's stored, and how it's retrieved.

--
Find common answers using Google Groups:

Corrupt MDBs FAQ
 
Just read your last post. I stick by my suggestion--learn what the program's already doing, then move onto changing it.
 
ok, i found a new table that lists all the records of my previous table, but with 1 big difference: it has a code or as was listed, a cardID with a 9 digit code folowed by name(?). Does this help with me not being able to see or having an image error because the record is not listed in this main table but is in the sub atble to this?
 
OK, let me explain exactly what is happening in the database.

1. SOMETHING, something you have not explained at all, is copying the contents of a file into those fields in the database. Thus you see the words "long binary data" in your OLE field.
2. SOMETHING, something you have also not explained at all, is able to retrieve and use this data.
3. For some reason, you want to add more files to your database, but don't know how to do so like the others already in the database.
4. You also don't know how to extract this data either.



I've been toying around with this some and here's some functions I downloaded (GetBlob and PutBlob) and some I wrote to use them (the run*() subs).

WARNING - read what the functions do before using them. I'm not responsible for you losing your data and losing your image files (both are probable if you mess up these functions - I lost a few image files sitting on my drive because I used the wrong function).

I have chosen not to explain the usage of these functions to you. Do the work, figure it out, adapt it to your needs.

The code is below.




Code:
Option Compare Database
Option Explicit

Private Const BlockSize = 32768

Private Const ERR_INVALID_FIELD_TYPE = vbObjectError + 1000
Function GetBlob(strFilename As String, fld As DAO.Field) As Long
'gets a file from disk (strFilename) into an OLE field (fld)
'the record with fld has to be in Edit or Append mode and needs to be Updated after
'returns the number of bytes read
'modified from MSKB Q210486
On Error GoTo Err_Handler
Dim intFile As Integer
Dim lngNumBlocks As Long, lngFileLength As Long
Dim lngLeftOver As Long, lngCount As Long
Dim abytFileData() As Byte

If Not Len(Dir$(strFilename)) > 0 Then GoTo Exit_Here

If Not fld.Type = dbLongBinary Then _
  Err.Raise ERR_INVALID_FIELD_TYPE, "GetBlob", "Field type has to be Long Binary (OLE)."

' Open the source file.
intFile = FreeFile
Open strFilename For Binary Access Read Lock Read Write As intFile

' Get the length of the file.
lngFileLength = LOF(intFile)
If Not lngFileLength > 0 Then GoTo Exit_Here
           
' Calculate the number of blocks to read and leftover bytes.
lngNumBlocks = Fix(lngFileLength / BlockSize)
lngLeftOver = lngFileLength Mod BlockSize

' Read the leftover data, writing it to the field.
abytFileData = StrConv(String$(lngLeftOver, vbNullChar), vbFromUnicode) 'String() gives a Unicode string, need to convert to ANSI
Get #intFile, , abytFileData
fld.AppendChunk abytFileData

' Read the remaining blocks of data, writing them to the table.
abytFileData = StrConv(String$(BlockSize, vbNullChar), vbFromUnicode)
For lngCount = 1 To lngNumBlocks
  Get intFile, , abytFileData
  fld.AppendChunk abytFileData
Next lngCount

GetBlob = lngFileLength
  
Exit_Here:
  On Error Resume Next
  Close intFile
  Exit Function
  
Err_Handler:
  MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
  Resume Exit_Here

End Function

Function PutBlob(strFilename As String, fld As DAO.Field) As Long
'writes the contents of an OLE field (fld) to a file on disk (strFilename)
'will silently overwrite existing file
'returns the number of bytes written
'modified from MSKB Q210486
On Error GoTo Err_Handler
Dim intFile As Integer, lngCount As Long
Dim lngFileLength As Long, lngLeftOver As Long, lngNumBlocks As Long
Dim abytFileData() As Byte

If Not fld.Type = dbLongBinary Then _
  Err.Raise ERR_INVALID_FIELD_TYPE, "GetBlob", "Field type has to be Long Binary (OLE)."

' Get the size of the field.
lngFileLength = fld.FieldSize()
If Not lngFileLength > 0 Then GoTo Exit_Here

' Calculate number of blocks to write and leftover bytes.
lngNumBlocks = Fix(lngFileLength / BlockSize)
lngLeftOver = lngFileLength Mod BlockSize

' Remove any existing destination file.
intFile = FreeFile
Open strFilename For Output As intFile
Close intFile

' Open the destination file.
Open strFilename For Binary Access Write Lock Write As intFile

' Write the leftover data to the output file.
abytFileData() = fld.GetChunk(0, lngLeftOver)
Put #intFile, , abytFileData()

' Write the remaining blocks of data to the output file.
For lngCount = 1 To lngNumBlocks
  ' Reads a chunk and writes it to output file.
  abytFileData() = fld.GetChunk((lngCount - 1) * BlockSize + lngLeftOver, BlockSize)
  Put #intFile, , abytFileData()
Next lngCount

PutBlob = lngFileLength
  
Exit_Here:
  On Error Resume Next
  Close intFile
  Exit Function

Err_Handler:
  MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
  Resume Exit_Here

End Function


Public Sub runit()
    Dim filename As String
    Dim rs As Recordset
    Dim lngFilePtr As Long
    
    filename = apiSaveFile("*.*")
    
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM t")
    
    rs.AddNew
    GetBlob filename, rs.Fields("blob")
    rs!Txt = "runit"
    rs.Update
    
    rs.Close
End Sub

Public Sub run2()
    Dim rs As Recordset
    
    Set rs = CurrentDb.OpenRecordset("SELECT * FROM t")
    
    Do While Not rs.EOF
        PutBlob apiSaveFile("*.*"), rs.Fields("blob")
    
        rs.MoveNext
    Loop
    
    rs.Close
End Sub
 
One additional thing: from reading this other thread ( Thread181-544816 ) I think you're missing your best source of information: the pre-existing code. There is already a function to retrieve your image data from your special OLE field. There is also already a function that adds files to a new record. It's all there in front of you -- check out your database and see what the other person did. Access/VBA code is nothing if not easily readable.

If you're trying to get a grasp of how a large chunk of code works, put in a breakpoint on the "button click" event and step through each step after the breakpoint. That's what I do when trying to figure out something in code.
 
Thx, I will check with pre-existing code. I am sure this will shed some light on my situation. The code you gave me where do i put that? Sorry I am new to this and am trying to learn on my own through an inhereted database. I should be able to just find all this info through Access(?).
 
This is not a plug-and-play situation with the code I provided. It is an example for one way to insert/extract binary data from OLE fields. Don't use it unless you're comfortable with it.


I still recommend you study the existing code. Maybe it's in the form's module, maybe it's in a separate code module.
 
Ok, the person who gave me the database is going to forward me some sample code. He also said the images were stored in as BLOB's. Does this help?
 
BLOBs are stored in OLE fields. It's what you already know.


The sample code will definitely help you.
 
oK, I been looking for the existing code, but no luck. not sure i am looking in right direction. also, the code u gave me, where do i put it? do i make a form and go into design view, and build an event for the image block and paste the code? i din't get the source code yet, waiting till tomorrow.
 
The original source is of huge importance.


If you want to try out the code I provided, I'd recommend you be very careful. I recommend you play around with the functions and try and insert some data; again, this isn't a plug-and-play situation. If you're getting the actual source tomorrow, I'd just do something else until then anyway. I got it to where it would insert and extract an item successfully, but will this work with your database's pre-existing BLOBs? Maybe, maybe not. All of that code I provided may be worthless.

Good Luck
 
You mentioned finding the source through access, but I had no luck. Where would I look?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top