Implementing a Database with the MS ADO Data Control (Visual Basic)

In Visual Basic, the LEADTOOLS OLEDB control is a OLEDB data bound control. This means you can associate the LEADTOOLS OLEDB control with an ADO data control so that the LEADTOOLS OLEDB control's bitmap is bound to a specified field in the data control's recordset.

Take the following steps to start a project and to add some code that associates the LEADTOOLS OLEDB control with an ADO data control:

1.

Start Visual Basic.

2.

On the Add-Ins menu, select the Visual Data Manager option.

3.

On the Data Manager window's Select File menu, select the New Database option.

4.

Specify a file name of leadpic.mdb and click the Save button.

5.

In the Database Manager window, click the New button.

6.

In the Add Table window, specify a table named: people, with the following two fields:

 

a.

Field name: who. Data Type: text. Size: 255

 

b.

Field name: photo. Data Type: Long Binary. Size: N/A.

7.

Close the Database Manager

8.

Add the LEAD RasterProcess Object Library to your project.

On the Project pull-down menu, use the References option, and select the LEAD RasterProcess Object Library (14.5).

9.

Add the LEAD RasterIO Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD RasterIO Object Library (14.5).

10.

Add the LEAD Raster Object Library and LEAD Raster Variant Object Library to your project.

 

On the Project pull-down menu, use the References option, and select the LEAD Raster Object Library (14.5).

 

On the Project pull-down menu, use the References option, and select the LEAD Raster Variant Object Library (14.5).

11.

Add the L_OCXERR.bas module to your project. (Found in the LEADTOOLS \Include folder.)

12.

On the Project pull-down menu, use the Components option to add the MS ADO data control to your project. Then add an ADO control to the bottom of your form and change its properties as follows:

 

a.

Set the ConnectionString property to connect to leadpic.mdb. (Use the browser to properly build the connection string using the OLEDB ODBC Provider.)

 

b.

Set the RecordSource by setting the CommandType to adCommandTable and the table to the "people" table.

 

c.

Set the Name property to Dpeople.

13.

On the Project pull-down menu, use the Components option to add the LEADRasterView Control module to your project.

14.

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

15.

On the Project pull-down menu, use the Components option to add the LEADTOOLS Raster OLE DB Custom Control module to your project. Select the LEADTOOLS Raster OLE DB control; then add the control to your main form.

16.

In the Properties box for the LEADTOOLS Raster OLE DB control, make the following changes:

 

a.

Set the DataSource property to Dpeople.

 

b.

Leave the DataMember property empty, in order to use the default for this data source.

17.

Add a TextBox control to your form and change its properties as follows:

 

a.

Set the DataSource property to Dpeople.

 

b.

Set the Name property to Cwho.

 

18.

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

 

Name

Caption

 

AddPhoto

Add Photo

 

DeletePhoto

Delete Photo

 

FlipPhoto

Flip Photo

19.

On the Project pull-down menu, use the Components option to add the LEAD Raster Common Dialog Control to your project.

20.

Add a LEAD Raster Common Dialog Control to your form.

21.

Add the following code to the form's Load procedure. In online help, you can use the Edit pull-down menu to copy the block of code.

'These are all persistent properties that can be set in the properties box.
LEADRasterView1.Appearance = RASTERVIEW_APPEARANCE_FLAT
LEADRasterView1.BorderStyle = 1
LEADRasterView1.BackColor = RGB(255, 255, 125)
LEADRasterView1.PaintDither = PAINTDITHER_DIFFUSION
LEADRasterView1.PaintPalette = PAINTPALETTE_AUTO
LEADRasterView1.AutoRepaint = True
LEADRasterView1.AutoSize = False
LEADRasterView1.AutoSetRects = True
LEADRasterView1.PaintSizeMode = PAINTSIZEMODE_FIT

'databind performed at design-time except for datafield
LEADRasterOLEDB1.Raster = LEADRasterView1.Raster
LEADRasterOLEDB1.DataField = "photo"
Cwho.DataField = "who"

' Set the data properties on the LEAD Raster OLE DB control
LEADRasterOLEDB1.DataLoadBits = 0
LEADRasterOLEDB1.DataSaveBits = 24
LEADRasterOLEDB1.DataSaveFormat = FILE_LEAD
LEADRasterOLEDB1.DataSaveQuality = QFACTOR_QMS
LEADRasterOLEDB1.EnableMethodErrors = True

22.

Code the AddPhoto button's Click event as follows:

Dim Myfile$
      Const BUFFERSIZE = 1024
      Dim buffer(BUFFERSIZE - 1) As Byte
      Dim RasterIO As New LEADRasterIO
      Dim RasterProc As New LEADRasterProcess
      Dim RasterVar As New LEADRasterVariant
      Dim RasterKrn As New LEADRasterDlgKrn
      Dim RasterFile As New LEADRasterDlgFile
      
      RasterKrn.InitDlg INIT_WITH_COLOR
      
   
   On Error GoTo ERRORHANDLER
      'release any reference
      LEADRasterView1.Raster.Bitmap = 0
   
      RasterFile.Filter = "Graphics|*.cmp;*.jpg;*.jff;*.jtf;*.bmp;*.tif;*.tga;*.pcx;*.cal;*.mac;*.mac;*.img;*.msp;*.wpg;*.wpg;*.ras;*.pct;*.pcd;*.eps;*.wmf"
      RasterFile.FileDlgFlags = 0 'cdlOFNFileMustExist
      RasterFile.DialogTitle = "Open File"
      RasterFile.UIFlags = OPEN_SHOW_FILEINFO + OPEN_SHOW_PREVIEW + OPEN_USEFILESTAMP
   
      LEADRasterDlg1.ShowOpenDlg hWnd
      Myfile = LEADRasterDlg1.FileName
      If Myfile = "" Then Exit Sub
   
      Dpeople.Recordset.AddNew
      RasterIO.Load LEADRasterView1.Raster, Myfile, 0, 1, 1
      LEADRasterOLEDB1.Raster = LEADRasterView1.Raster
      LEADRasterOLEDB1.DataDirty = True
      Cwho = Myfile
      Dpeople.Recordset.Update
   
      Open Myfile For Binary Access Read As #1
      Do
          Get #1, , buffer
          Dpeople.Recordset!photo.AppendChunk buffer
      Loop Until EOF(1) 
   
      Close #1
   
      RasterIO.StartFeedLoad LEADRasterView1.Raster, 0, 1, 1
      Do
          RasterVar.Type = VALUE_STRING
          RasterVar.ShortValue = Dpeople.Recordset!photo.GetChunk(BUFFERSIZE) 
           
          If RasterVar.Type <> vbNull Then
              RasterIO.FeedLoad RasterVar, BUFFERSIZE
          End If
      Loop Until RasterVar.Type = vbNull
   
      RasterIO.StopFeedLoad
   
      ' Requery the data control's recordset
      Dpeople.Recordset.Requery
   Exit Sub
   
ERRORHANDLER: 
   If Dpeople.Recordset.EditMode = dbEditAdd Then
       Dpeople.Recordset.CancelUpdate
   End If
   If (Err.Number <> ERROR_DLG_CANCELED) Then
      MsgBox Err.Source + " " + CStr(Err.Number) + Chr(13) + Err.Description
   End If
   
   RasterKrn.FreeDlg

23.

Code the DeletePhoto button's Click event as follows:

Dpeople.Recordset.Delete
Dpeople.Recordset.MoveFirst

24.

Code the FlipPhoto button's Click event as follows:

RasterProc.Flip LeadRasterView1.Raster
LEADRasterOLEDB1.Raster = LEADRasterView1.Raster
LEADRasterOLEDB1.DataDirty = True

25.

Add the following code to the LEAD Raster OLEDB control's DataLoaded event:

If (nStatus = 0) Then
    LEADRasterView1.ForceRepaint
    LEADRasterOLEDB1.DataDirty = False
ElseIf (nStatus = ERROR_FILENOTFOUND) Then 'if it's an empty record
    LEADRasterView1.Raster.Bitmap = 0
ElseIf (nStatus <> ERROR_FILENOTFOUND) Then 'if it's not an empty record
    LEADRasterView1.Raster.Bitmap = 0
    MsgBox "Error loading from database!"
End If
LEADRasterView1.ForceRepaint
LEADRasterOLEDB1.DataDirty = False

26.

Add the following code to the LEAD Raster OLEDB control's DataSaved event:

If (nStatus <> 0) Then
    LEADRasterView1.Raster.Bitmap = 0 'free the current bitmap
    LEADRasterView1.ForceRepaint
    MsgBox "Error saving to database!"
End If

27.

Run your program to test it.