Loading and Saving Annotations Using Databases (Visual Basic)

Note: This topic is for Document/Medical only.

Take the following steps to start a project and to add some code that demonstrates adding annotation objects to or deleting annotation objects 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.UnlockSupport L_SUPPORT_DOCUMENT, L_KEY_DOCUMENT
   LEAD1.Load "c:\parrots.jpg", 0, 0, 1
   
   LEAD1.AnnUserMode = ANNUSERMODE_DESIGN
      
   LEAD1.Visible = True
   LEAD1.AnnToolbar.Create 0, 0, ANNTOOLALIGN_TOP
   LEAD1.AnnToolbar.Visible = True

   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 AnnVar As Variant
   Dim ByteArray() As Byte
   Dim i As Integer
   Dim nSize As Integer
   
   If Connection.State = adStateClosed Then
      MsgBox "Connection closed", vbInformation, "Save error"
      Exit Sub
   End If

   Recordset.AddNew
   
   LEAD1.AnnSaveArray AnnVar, ANNFMT_NATIVE, False, SAVE_OVERWRITE, 0

   nSize = UBound(AnnVar) - LBound(AnnVar) – 1
   Recordset.Fields("Image").Value = AnnVar
   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 Integer
   Dim nSize As Integer
   Dim AnnVar As Variant
   
   If Not Recordset.EOF Then Recordset.MoveNext
   
   If (Recordset.EOF) Then
      MsgBox "Unable to get the annotation from the database"
      Recordset.MovePrevious
   Else
      nSize = Recordset.Fields("Size").Value
      AnnVar = Recordset.Fields("Image").Value
      LEAD1.AnnLoadArray AnnVar, nSize, 1
   End If

10.

Code the cmdMovePrevious button's Click procedure as follows:

   Dim AnnVar As Variant
   Dim i As Integer
   Dim nSize As Integer
   
   If Not Recordset.BOF Then Recordset.MovePrevious
   
   If (Recordset.BOF) Then
      MsgBox "Unable to get the annotation from the database"
      Recordset.MoveNext
   Else
      nSize = Recordset.Fields("Size").Value
      AnnVar = Recordset.Fields("Image").Value
      LEAD1.AnnLoadArray AnnVar, nSize, 1
   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.