Loading and Saving Images Using Databases (Visual Basic)

Take the following steps to start a project and to add some code that demonstrates adding images to or deleting images from the BLOB field of a database file.

You need to create a database file (DB1.MDB) with the fields listed below, before running this tutorial.

Field Name

Type

Image

OleObject

Size

Number

1.

Start Visual Basic.

2.

Add the LEAD Main Control to your project.

 

On the Project pull-down menu, use the Components option, and select the LEAD Main ActiveX Control (16).

3.

Add the Microsoft ActiveX Data Object 2.6 Library to your project.

 

On the Project pull-down menu, use the References option, and select the Microsoft ActiveX Data Object 2.6 Library.

4.

Select the LEAD Main control; then add the control to your main form. Size and position the control as you want it to appear at run time.

5.

Add six command buttons to your form and name them as follows:

 

Name

Caption

 

cmdAddRecord

Add Record

 

cmdMoveNext

Move Next

 

cmdMovePrevious

Move Previous

 

cmdDeleteRecord

Delete Record

 

cmdConnect

Connect to Database

 

cmdDisconnect

Disconnect From Database

 

 

 

6.

Declare the following variables as Global variables:

Dim Recordset As New ADODB.Recordset
Dim Connection As New ADODB.Connection
Dim strConnect As String

 

7.

Add the following initialization code to the main form's Load procedure.

   LEAD1.Load "c:\parrots.jpg", 0, 0, 1
  
   cmdMoveNext.Enabled = False
   cmdMovePrevious.Enabled = False
   cmdAddRecord.Enabled = False
   cmdDeleteRecord.Enabled = False
   
   strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "\DB1.mdb"

 

8.

Code the cmdAddRecord button's Click procedure as follows:

   Dim Data As Variant
   Dim ByteArray() As Byte
   Dim i As Long
   Dim nSize As Long
   
   If Connection.State = adStateClosed Then
      MsgBox "Connection closed", vbInformation, "Save error"
      Exit Sub
   End If

   Recordset.AddNew
   
   Data = LEAD1.SaveArray(FILE_BMP, 0, 0)
   nSize = LEAD1.ArraySize(Data)
   Recordset.Fields("Image").Value = Data
   Recordset.Fields("Size").Value = nSize
   
   If Not Recordset.BOF Then Recordset.MoveFirst
   
   Recordset.Update
   
   If Recordset.RecordCount > 0 Then
    cmdMoveNext.Enabled = True
    cmdMovePrevious.Enabled = True
    cmdDeleteRecord.Enabled = True
   End If

 

9.

Code the cmdMoveNext button's Click procedure as follows:

   Dim i As Long
   Dim nSize As Long
   Dim Data As Variant
   
   If Not Recordset.EOF Then Recordset.MoveNext
   
   If (Recordset.EOF) Then
      MsgBox "Unable to get the image from the database"
      Recordset.MovePrevious
   Else
      nSize = Recordset.Fields("Size").Value
      Data = Recordset.Fields("Image").Value
      LEAD1.LoadArray Data, 0, 0, -1, nSize
   End If

 

10.

Code the cmdMovePrevious button's Click procedure as follows:

   Dim Data As Variant
   Dim i As Long
   Dim nSize As Long
   
   If Not Recordset.BOF Then Recordset.MovePrevious
   
   If (Recordset.BOF) Then
      MsgBox "Unable to get the image from the database"
      Recordset.MoveNext
   Else
      nSize = Recordset.Fields("Size").Value
      Data = Recordset.Fields("Image").Value
      LEAD1.LoadArray Data, 0, 0, -1, nSize
   End If

 

11.

Code the cmdDeleteRecord button's Click procedure as follows:

     If Connection.State = adStateClosed Then
      MsgBox "Connection closed", vbInformation, "Delete error"
      Exit Sub
   End If
   If Not Recordset.BOF Or Not Recordset.EOF Then
      Recordset.MoveLast
      Recordset.Delete
      Recordset.Update
      
      If Recordset.RecordCount <= 0 Then
         cmdMoveNext.Enabled = False
         cmdMovePrevious.Enabled = False
         cmdDeleteRecord.Enabled = False
      End If
   End If

 

12.

Code the cmdConnect button's Click procedure as follows:

       If Connection.State = adStateClosed Then
         Connection.Open strConnect
      End If
   
      If Recordset.State = adStateOpen Then Recordset.Close
      Set Recordset.ActiveConnection = Connection
      Recordset.Open "SELECT * FROM Images", Connection, adOpenKeyset, adLockOptimistic

      If Not Recordset.BOF Then Recordset.MoveFirst
   
      If Not Recordset.EOF Then
         cmdMoveNext.Enabled = True
         cmdMovePrevious.Enabled = True
         cmdDeleteRecord.Enabled = True
      End If
      
      cmdAddRecord.Enabled = True

 

13.

Code the cmdDisconnect button's Click procedure as follows:

      If Connection.State = adStateOpen Then Connection.Close
      If Recordset.State = adStateOpen Then Recordset.Close
      cmdMoveNext.Enabled = False
      cmdMovePrevious.Enabled = False
      cmdAddRecord.Enabled = False
      cmdDeleteRecord.Enabled = False

 

14.

Run your program to test it.