Solved

VB6: Moving pictures into a database.

Posted on 1998-09-30
4
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

15 Experts available now in Live!

Get 1:1 Help Now