View Various Tables (IBX General Tutorial)
Go Up to Tutorial: Using InterBase Express to Access a Database
This section creates a form that allows you to view different database tables. It uses the EMPLOYEE database, which is installed with RAD Studio.
The form looks like this after the components have been placed:
Contents
Create the Form
Begin by adding another VCL Form to the project. In the Project Manager, right-click the project and click the menu item Add New > VCL Form.
Adjust the new form's properties:
- Set Caption to "Table/View Switcher".
- Set Name to "FrmViewDemo".
- Change the form's Height to 330 and its Width component to 400.
- Set OldCreateOrder to true.
Save the form:
- For Delphi, save the file as FrmViews.pas.
Add Database Components
Add database components first. Later, you configure the data-aware components to get data from the database components. Since database components are nonvisual, you can place them anywhere on the form, but you should place them so they do not obscure the visual components.
- Drag a TIBDatabase, which represents an InterBase database connection, onto the form. Double-click the new TIBDatabase to display the Database Component Editor. Set the TIBDatabase's properties by filling out the dialog:
- In this tutorial, you use the EMPLOYEE database that is installed with RAD Studio. Its path is typically
C:\Users\Public\Documents\Embarcadero\Studio\17.0\Samples\Data\EMPLOYEE.GDB
. If you set the user name and password, LoginPrompt can be false.
Note: If you embed the password in the application, as in this example, anyone has access to this database without logging in.
- Add a TIBTransaction to the form. Set the DefaultTransaction property of the TIBDatabase to "IBTransaction1".
- Place a TIBDataSet on the form. Set the Transaction property to "IBTransaction1". Connect this TIBDataSet to the database by setting its Database property to "IBDatabase1" using the drop-down menu. Set the Name property to "VaryingDataSet".
- Add a TDataSource to the form. This component serves as an interface between the TIBDataSet and data-aware controls. Set TDataSource's DataSet to "VaryingDataSet" with the drop-down menu. Set the Name property to "VaryingTableSource".
Add Visual Components
Now add the components that display database data, connecting them to the database components.
Add two TPanel components, positioning them as shown in the figure. These TPanels provide a framework for positioning the visual components. Position them as shown in the figure, filling up the form.
Add components to the "Panel1" TPanel.
- Place a TDBNavigator on "Panel1" to navigate database records. Set DataSource to "VaryingTableSource". The TDataSource serves as an interface between the TIBDataSet and data-aware controls.
- Add a TSpeedButton to "Panel1".
- To set the button face, in the Object Inspector for the TSpeedButton, click the ellipsis (...) button next to the Glyph property to display the Picture Editor dialog. Click the Load... button to choose a bitmap file for the button face. You can obtain a bitmap in the desired format from the CsDemos sample in the TSpeedButtons in the Frmviews unit. Select the TSpeedButton, open the Picture Editor, click Save... and save to a bitmap file.
- Set GroupIndex to 1.
- Set Name to "BtnShowEmployeeClick".
- Add another TSpeedButton and place it right next to the other TSpeedButton. Set its Glyph and GroupIndex properties as you did for the other TSpeedButton.
- Place a TBitBtn on the right side of "Panel1". Set the following properties:
- Set Glyph as you did for the TSpeedButtons.
- Set Kind to "bkClose" from the drop-down menu. This setting makes the button execute a command to close the dialog when you click the TBitBtn.
- Set Caption to "E&xit", so that pressing
ALT+x
is the same as clicking the button. - Set Name to "BtnShowPhoneListClick".
Add a component to the "Panel2" TPanel.
- Place a TDBGrid on "Panel2". Resize it to fill the TPanel. Set DataSource to "VaryingTableSource" from the drop-down menu.
Add Code
Complete the form by adding one routine and creating event handlers for the TSpeedButtons. You don't need an event handler for the TBitBtn, because this button already has a command associated with clicking it.
Add a ShowTable routine that shows the indicated table.
Delphi
Add this procedure in the private
part of the type
section of FrmViews.pas:
procedure ShowTable(ATable: string);
You can use class completion by pressing CTRL-SHIFT-C
to create a stub for this function in the implementation
section.
Add this code for the new procedure:
procedure TFrmViewDemo.ShowTable( ATable: string );
begin
Screen.Cursor := crHourglass; { show user something's happening }
VaryingDataSet.DisableControls; { hide data changes from user }
VaryingDataSet.Active := FALSE; { close the table }
VaryingDataSet.SelectSQL.Text:= 'SELECT * FROM '+ ATable; { update the name }
VaryingDataSet.Open; { open the table }
VaryingDataSet.EnableControls; { paint the changes }
Screen.Cursor := crDefault; { reset the pointer }
end;
ShowTable
performs all the actions needed to display the given table's data. In particular, ShowTable
sets TIBDataSet SelectSQL to select data from the current table.
Finally, add the event handlers. In the Design tab, double-click the first TSpeedButton. This adds a prototype for the event handler and creates stub code for it. Place the following code in the implementation
section for the procedure:
procedure TFrmViewDemo.BtnShowEmployeeClick(Sender: TObject);
begin
ShowTable('EMPLOYEE');
end;
In a similar fashion, add an event handler for the second TSpeedButton, using the code:
procedure TFrmViewDemo.BtnShowPhoneListClick(Sender: TObject);
begin
ShowTable('PHONE_LIST');
end;
These routines simply call ShowTable
to set the table you view.
Display the Table Viewing Form
Modify the main form created in Create Main Form so that the button on that form displays the form you just created.
Delphi
Add the following to the beginning of the implementation
section of Frmmain.pas so the main form knows about the FrmViews unit you just added:
uses
FrmViews; { The View Demo }
Next, add an event handler for the Main form's button by double-clicking that button in the Design tab of Frmmain.pas. Use the following code for the event handler:
procedure TFrmLauncher.BtnViewsClick(Sender: TObject);
begin
FrmViewDemo.ShowModal;
end;
ShowModal displays the form as a modal dialog, so the application doesn't run until the dialog closes. In this case, the main dialog is on hold as long as the table viewing form is visible.
Run the Application
Build and run the application. The main form displays:
Click the button to display the table viewing form:
Notice that the TDBGrid is empty since no table has been selected. Click the left TSpeedButton to display the EMPLOYEE table:
Clicking the other TSpeedButton shows the PHONE_LIST table.
Clicking the TBitBtn marked Close closes the table viewing form.