Solved

VB6: Moving pictures into a database.

Posted on 1998-09-30
4
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

624 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