Lookup Controls

The Necessary Tables

  1. Start Database Desktop
  2. To create a new table, on the main menu, click File -> New -> Table...
  3. Create the table as follows:
     
    Create Paradox 7 Table
     
    If you are using Corel Paradox 10, create the table as follows:
     
  4. To save the table, click the Save As or the Create button.
  5. Click the arrow of the Alias combo box and select BCDEMOS
  6. In the File Name edit box, type MusicAlbums
     
  7. Click Save and close the table.
  8. Reopen the table and enter a few records

Basic Forms

  1. If you didn't yet, start Borland C++ Builder or Delphi.
    Save the project as MusicCollection in a new folder created as ExoMusic
  2. Save the unit as Main
  3. Design the starting form as follows:
     
  4. To use a module, on the Standard toolbar, click the New button
  5. In the New Items dialog box, click Data Module and click OK
  6. Change its Name to MusicModule and save it as DMMusic
  7. From the BDE tab of the Component Palette, click Table and click on the module window
  8. Change its Name to AlbumsTable
  9. Using the Object Inspector, set the DatabaseName to BCDEMOS
  10. Set the TableName to MusicAlbums and set the Active property to true or True
  11. From the Data Access tab of the Component Palette, click DataSource and click in the module window
  12. Change its Name to AlbumsSource
     
  13. Using the Object Inspector, set the DataSet to AlbumsTable
  14. Select the form. On the main menu, click File -> Include Unit Hdr...
  15. From the Use Unit dialog box, make sure DMMusic is selected and click OK
  16. On the form, select all DB* controls  and the DBNavigator bar
  17. Using the Object Inspector, set their DataSource to MusicModule->AlbumsSource
  18. Set each DB* control (excluding the DBNavigator, of course) to its corresponding DataField. The DBMemo1 must be set to Notes
  19. Test the application and return to Bcb or Delphi

Creating a Relationship Table

  1. Get back to Database Desktop.
    To create another table, on the main menu of Paradox, click File -> New -> Table
  2. Create the table as follows:
     
  3. Save the table as MusicTracks in the BCDEMOS alias.

 

Preparing Grid Lookup Fields

  1. Get back to Bcb or Delphi and display the MusicModule window
  2. Click AlbumsTable and, using the Object Inspector, set its Active property to false
  3. From the BDE tab of the Component Palette, click Table and click in the module window
  4. Change the Name of the new table to TracksTable
  5. Set its DatabaseName to BCDEMOS. Set its TableName to MusicTracks and make sure its Active property is set to false
  6. Double-click the TracksTable icon. This opens the Field Editor
  7. Right-click in the Fields Editor and click Add All Fields
  8. To create the field that will be used to select an album, right-click the Fields Editor again and click New Field
  9. In the Name edit box, type Album
  10. In the Type combo box, select String and, in the Size edit box, type 50
  11. In the Field Type section, click the Lookup radio button
  12. Set the Key Field to AlbumID
  13. Set the Dataset to AlbumsTable
  14. Set the Lookup Keys to AlbumID
  15. Set the Result Field to AlbumTitle
     
  16. Click OK and close the Fields Editor
  17. Set the Active property of both tables to true
  18. From the Data Access tab of the Component Palette, click DataSource and click in the module window
  19. Change its Name to TracksSource and set its DataSet to MusicTracks

Lookup Fields on a DBGrid

  1. To create a new form, in Bcb or Delphi, on the View toolbar, click the New Form button
  2. Change the Name to frmTracks and save its unit as AlbumTracks
  3. Add a DBGrid to the form and design it as follows:
     
  4. On the main menu, click File -> Include Unit Hdr...
  5. From the list of units, click DMMusic and click OK
  6. On the form, click the DBGrid and, using the Object Inspector, set its Data Source to MusicModule->TracksSource
  7. Right-click the DBGrid and click Columns Editor
  8. Right-click in the Columns Editor and click Add All Fields
  9. Click TrackID in the Columns Editor and, using the Object Inspector, set its Visible property to false
  10. In the same way, set the AlbumID's Visible property to false
  11. In the Columns Editor, right-click Notes and click Delete
  12. In the Columns Editor, drag Album up and position it between TrackNumber and TrackTitle
     
  13. Save everything
  14. Display the main form and, on the main menu, click File -> Include Unit Hdr...
  15. Make sure AlbumTracks is selected and click OK
  16. Add a button on the right side of the existing controls
  17. Change its name to btnTracks and its Caption to &Tracks
  18. Implement its OnClick event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::btnTracksClick(TObject *Sender)
    {
        frmTracks->ShowModal();
    }
    //---------------------------------------------------------------------------
  19. Test the application:
     
  20. Return to Bcb or Delphi

Preparing List Lookup Fields

  1. Close Bcb or Delphi and open (or return to) Paradox
  2. Create a new table as follows:
     
  3. Save the table as MusicCategories in the BCDEMOS alias
  4. Enter a few music categories and close the table:
     
  5. Reopen the MusicAlbums table (in Paradox) and on the main menu, click Table -> Restructure...
  6. Click Label and press Insert
  7. Add the MusicCategoryID field as follows:
     
  8. Save and close the table

 

 

Using DBLookupComboBox or DBLookupListBox

This shows how to use the TDBLookupCombobox and the TDBLookupListBox database controls.

  1. If necessary, start Bcb or Delphi. Open the MusicCollection project
  2. Display the MusicModule window
  3. From the BDE tab of the Component Palette, click Table and click in the module window
  4. Change its Name to CategoriesTable
  5. Set its DatabaseName to BCDEMOS and set its TableName to MusicCategories
  6. From the Data Access tab of the Component Palette, click DataSource and click in the module window
  7. Change its Name to CategoriesSource and set its DataSet to Categories
     
  8. Add a new form
  9. Set its Name to frmCategories and save it as MusicCategories
  10. Design it as follows:
     
  11. In the module window, click AlbumsTable to select it.
  12. Using the Object Inspector, set its Active property to false
  13. In the module window, right-click AlbumsTable and click Fields Editor
  14. Right-click in the Fields Editor and click Add Fields
  15. Make sure the new field is selected and click OK
  16. Right-click the Fields Editor and click New Fields...
  17. Fill out the dialog box as follows:
     
  18. Click OK and close the Fields Editor
  19. Display the main form.
  20. From the Data controls tab of the Component Palette, click DBLookupComboBox and add it to the form
  21. Set its DataSource to MusicModule->AlbumsSource and set its DataField to Category:
     
  22. Using the module window and the Object Inspector, set all tables Active property to true
  23. Test the application
     
  24. Close everything

Copyright 2003-2007 FunctionX, Inc.