Solved

VB6: Moving pictures into a database.

Posted on 1998-09-30
4
234 Views
Last Modified: 2010-04-30
I've got an app that uses a database.  This mdb file contains a directory that pretty much contains the path names to a bunch of images.  I am now to the point where I would like to simply embed each of these images into the mdb file itself.

How can I embed these files from code knowing only the path names leading to each file?

Lankford
0
Comment
Question by:lankford
  • 2
4 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1437477
I'd imagine that you'ld have to use a MEMO data type and copy the image into it as a byte stream.

Only problem is that the images in the database aren't going to do you much good. You can't point to a field in a database in a LoadImage statement so if you want to *show* the images you'll have to pull the data back out of the database and recreate a file on disk as a temporary giz so you can then load the disk file back in to the picture control. This'll be slow as mud.

M
0
 

Author Comment

by:lankford
ID: 1437478
I've got an image control that will bind to this OLE Bindary data.  I don't have to worry about extraction, just insertion!
0
 
LVL 14

Accepted Solution

by:
waty earned 50 total points
ID: 1437479
Use the following function :

Function SaveFileToDB(Source As String, BynaryField As Field) As Integer
   ' *** Will save a file to a Binary field or memo field

   Dim nNumBlocks       As Integer
   Dim nFile            As Integer
   Dim nI               As Integer
   Dim nFileLen         As Long
   Dim nLeftOver        As Long
   Dim sFileData        As String
   Dim RetVal           As Variant
   Dim nBlockSize       As Long

   On Error GoTo Error_SaveFileToDB

   nBlockSize = 32000    ' Set size of chunk.

   ' *** Open the source file.
   nFile = FreeFile
   Open Source For Binary Access Read As nFile

   ' *** Get the length of the file.
   nFileLen = LOF(nFile)
   If nFileLen = 0 Then
      SaveFileToDB = 0
      Exit Function
   End If

   ' *** Calculate the number of blocks to read and nLeftOver bytes.
   nNumBlocks = nFileLen \ nBlockSize
   nLeftOver = nFileLen Mod nBlockSize

   ' *** Read the nLeftOver data, writing it to the table.
   sFileData = String$(nLeftOver, 32)
   Get nFile, , sFileData
   BynaryField.AppendChunk (sFileData)

   ' *** Read the remaining blocks of data, writing them to the table.
   sFileData = String$(nBlockSize, Chr$(32))
   For nI = 1 To nNumBlocks
      Get nFile, , sFileData
      BynaryField.AppendChunk (sFileData)
   Next
   Close #nFile
   SaveFileToDB = nFileLen

   Exit Function

Error_SaveFileToDB:
   SaveFileToDB = -Err
   Exit Function
End Function

0
 

Author Comment

by:lankford
ID: 1437480
I found a much better way to do it.  Turns out that the control I am binding to the data source that displays the graphic will load the information into the database for you too. When loading the graphics, the current record then gets the binary data associated with that loaded file.  Pretty sweet and only one line of code.

I did try your code.  The image control would not read in the binary data and translate it into an image.  I do not know why and don't really have the time to find out.  

So, just to cut this short, I'll give you a C for an answer that may or may not have solved my problems.

Thank you for the reply.

Lankford
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now