Solved

Saving/Reading images in SQL7 w/ VB6

Posted on 2000-04-10
1
223 Views
Last Modified: 2010-05-02
How do I save an image that the user has dropped into an OLE control to a SQL7 database.  Also, how do I query that db and display the image in the ole control? What is the property of the OLE control that relates to the image, .data?  I am using ADO 2.1 and 3 tiers architecture.  
0
Comment
Question by:BigJimFrog
1 Comment
 
LVL 1

Accepted Solution

by:
scj1 earned 100 total points
ID: 2723882
I've found the answer to your problem....(and mine). Okay some of this code has been used from an answer back in 1999 which came from an example that originally used Oracle. This example connects to whatever open recordset you pass in, with the picture field name.


Method 1: SetPicture puts a picture into the database (this assumes a bitmap file - you can save your OLE image first if you want or modify the code to read the bytes from a IPictureDisp object). To save the picture use the following code:

savePicture(picture1.picture,"d:\test.bmp")

Method 2: GetPicture retrieves the picture from the current record from a passed in recordset. It does use a temporary file to write out the picture object. It returns an IPictureDisp object. To set the picture property of a picture control you would use the following command:

set picture1.picture = getPicture("picFieldName",rsRecSetThatIsOpenObject")

I hope that helps.

The code:

Public Function setPicture(ByVal strPicturePath As String, strPicField As String, ByVal rs As ADODB.Recordset) As Boolean
  ' note: this requires the record to already exist - it will insert the
  ' picture at the current position in the recordset
  ' Returns true if success - false otherwise
  Const BlockSize = 100000
 
  Dim ByteData() As Byte   'Byte array for Blob data.
  Dim SourceFile As Integer
 
  ' Save Picture image to the table column.
  SourceFile = FreeFile
  Open strPicturePath For Binary Access Read As SourceFile
  FileLength = LOF(SourceFile)  ' Get the length of the file.
 
  If FileLength = 0 Then
    Close SourceFile
    setPicture = False
    Exit Function
  Else
    Numblocks = FileLength / BlockSize
    LeftOver = FileLength Mod BlockSize
    ReDim ByteData(LeftOver)
    Get SourceFile, , ByteData()
    rs(strPicField).AppendChunk ByteData()
    ReDim ByteData(BlockSize)
    For i = 1 To Numblocks
      Get SourceFile, , ByteData()
      rs(strPicField).AppendChunk ByteData()
    Next i
    rs.Update   'Commit the new data.
    Close SourceFile
    setPicture = True
  End If
End Function

Public Function getPicture(strPicField As String, ByVal rs As ADODB.Recordset) As IPictureDisp
  Const BlockSize = 100000
  Dim ByteData() As Byte   'Byte array for picture file.
  Dim DestFileNum As Integer
  Dim DiskFile As String

  ' Remove any existing destination file.
  DiskFile = App.Path & "\temp.bmp"
  If Len(Dir$(DiskFile)) > 0 Then
     Kill DiskFile
  End If
 
  DestFileNum = FreeFile
  FileLength = rs(strPicField).ActualSize
  Open DiskFile For Binary As DestFileNum
 
  Numblocks = FileLength / BlockSize
  LeftOver = FileLength Mod BlockSize
   
  rs.Move 0, adBookmarkCurrent
  ByteData() = rs(strPicField).GetChunk(LeftOver)
  Put DestFileNum, , ByteData()
 
  For i = 1 To Numblocks - 1
      ByteData() = rs(strPicField).GetChunk(BlockSize)
      Put DestFileNum, , ByteData()
  Next i
 
  Close DestFileNum

  Set getPicture = LoadPicture(App.Path & "\temp.bmp")
End Function
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now