Implementing a Database with a Data Control (Visual Basic)

In Visual Basic, the LEAD control is a data bound control. This means you can associate the LEAD control with a data control so that the LEAD 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 LEAD control with a data control:

1. Start Visual Basic.

2. On the Add Ins menu, select the 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 a data control to the bottom of your form and change its properties as follows:

a. Set the DatabaseName property to leadpic.mdb. (Use the browser to get the full path name.)

b. Set the RecordSource property to people.

c. Set the Name property to Dpeople.

9. On the Tools pull-down menu, use the Custom Controls option to add the LEAD OLE Custom Control module to your project.

10. image\btnlead.gif Select the LEAD control; then add the control to your main form. Stretch and position the control as you want it to appear at run time.

11. In the Properties box for the LEAD control, make the following changes:

a. Set the DataSource property to Dpeople.

b. Set the DataFieldproperty to photo.

c. Set the Name property to Lead1.

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

a. Set the DataSource property to Dpeople.

b. Set the DataField property to who.

c. Set the Name property to Cwho.

13. image\btncmd.gif Add three command buttons to your form and name them as follows:

Name

Caption

AddPhoto

Add Photo

DeletePhoto

Delete Photo

FlipPhoto Flip Photo

14. image\btndlg.gif Add a CommonDialog control to your form.

15. 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.
Lead1.Appearance = APPEARANCE_FLAT
Lead1.BorderStyle = 1
Lead1.BackColor = RGB(255, 255, 125)
Lead1.PaintDither = PAINTDITHER_DIFFUSION
Lead1.PaintPalette = PAINTPALETTE_AUTO
Lead1.AutoRepaint = True
Lead1.AutoSize = False
Lead1.AutoSetRects = True
Lead1.PaintSizeMode = PAINTSIZEMODE_FIT
Lead1.DataLoadBits = 0

16. Code the AddPhoto button's Click event as follows:

On Error GoTo ERRORHANDLER
  CommonDialog1.Filter = "Grapics|*.cmp;*.jpg;*.jff;*.jtf;*.bmp;*.tif;*.tga;*.pcx;*.cal;*.mac;*.mac;*.img;*.msp;*.wpg;*.wpg;*.ras;*.pct;*.pcd;*.eps;*.wmf"
  CommonDialog1.Flags = cdlOFNFileMustExist
  CommonDialog1.DialogTitle = "Open File"
  CommonDialog1.CancelError = True
  CommonDialog1.ShowOpen
  MyFile = CommonDialog1.filename
  Dpeople.Recordset.AddNew
  Lead1.Load MyFile, 0, 0, 1
  Cwho = MyFile

  ' Specify the appropriate properties for saving the image in the database
  If Lead1.BitmapBits = 1 Then
    Lead1.DataSaveBits = 1
    Lead1.DataSaveFormat = FILE_LEAD1BIT
  ElseIf Lead1.BitmapBits = 4 Then
    Lead1.DataSaveBits = 4
    Lead1.DataSaveFormat = FILE_PCX
  ElseIf Lead1.IsGrayscale = GRAY_NO Then
    Lead1.DataSaveBits = 24
    Lead1.DataSaveFormat = FILE_CMP
    Lead1.DataSaveQuality = QFACTOR_QMS
  Else 'save as grayscale
    Lead1.DataSaveBits = 8
    Lead1.DataSaveFormat = FILE_CMP
    Lead1.DataSaveQuality = QFACTOR_QMS
  End If

  ' Force an update
  Dpeople.Recordset.MoveLast
Exit Sub

ERRORHANDLER:
If Dpeople.Recordset.EditMode = dbEditAdd Then
    Dpeople.Recordset.CancelUpdate
End If
MsgBox Err.Source + " " + CStr(Err.Number) + Chr(13) + Err.Description

17. Code the DeletePhoto button's Click event as follows:

Dpeople.Recordset.Delete
Dpeople.Recordset.MoveFirst

18. Code the FlipPhoto button's Click event as follows:

Lead1.Flip

19. Add the following code to the Lead1 control's Change event:

' Avoid processing events that occur before the bitmap is fully loaded
If Lead1.Bitmap = 0 Then Exit Sub
' Demonstrate how the DataChanged property works
If Lead1.DataChanged And Dpeople.Recordset.EditMode <> dbEditAdd Then
        MsgBox "You flipped the photo"
End If

20. Run your program to test it.