Make your own free website on Tripod.com






[an error occurred while processing this directive]



3. Creating a database access form

To create a form connected to a database it is possible to use C++ Builder's Database Form Wizard or to proceed manually by adding each necessary component. This first time we'll use the C++ Builder's Wizard. Select the New... option in the File mený, open the window Forms an click on the Database Form icon. DataBase Form Wizard will start (fig.3.1).





fig.3.1: C++ Builder Database Form Wizard



First of all we are asked to chose which kind of form we want to create. We'll chose the Create simple form option and Create a form using a TTable object. Then click on next and specify the absolute path of the tables we want to use. By default the Database Desktop saves its data in the "...\Borland\Database Desktop\" directory. Move in that directory and select Person.DB. then we will choose the fields we want to be displayed in the form (select all the fields) and the form's layout (select Horizontal). In the last window de-select the option Generate a main form and choose the Form only option. All steps described are shown in fig.3.2 - 3.5. By clicking on Finish button our new database form will be ready.





fig.3.2: Step 1




fig.3.3: Step 2




fig.3.4: Step 3




fig.3.5: Step 4



Make the Object Inspector visible (pressing F11), select the new form and change the Name property to PersonForm, then click on Save All from the File mený to save it. We will be asked for the name to use to save PersonForm's code. Write PersonUnit. Looking at PersonForm we'll note in it many TLabel and TDBEdit type object. TLabel objects are static text, while TDBEdits are textfield in which data are shown. Each TDBEdit is connected to the datasource component that gets data from Person.DB table. It is important to note in the window the presence of a Datasource and a Table object. These are not visible components, I mean, these components will not be displayed at executin time, but they are necessary to create and to manage the connection between the external table Person.DB and the TDBEdit objects. The buttons on the top of the window belongs to a single component: the DBNavigator. Clicking on the DBNavigator, at execution time, the user is allowed to choose which record to show and editing/adding/deleting records. It is possible to change form's layout as we prefer just moving the components or changing their dimensions. Obviously it makes no sense to move components like a Table or a Datasource because of the fact they are not visible at execution time. Modify the Caption property of the form named PersonForm (using the Object Inspector) in Person to have this text shown on the title bar. Change TLabels' Caption property too where it is necessary. Remember that selecting a component in the form and pressing F11 the Object Inspector releated to that object will be displayed. Completed this operations, the form apperance will be like the one in fig.3.6.





fig.3.6: Il form Persone



Now it is necessary to connect the forms we have created each other. We'll do it adding a new button in each form and writing the necessary code for the OnClick events. This will allow the user to move from the BasicForm to the PersonForm and viceversa. Add a TBitBtn button in each form modifing the Caption property with the value Person in the BasicForm's button and Back in the PersonForm's one. To make a form visible at projecting time, we can use the option Forms... from the View mený, or using the button shown in fig.3.7.





fig.3.7: Button to press to show a form




DoubleClicking on the button we have just added to the form, the funcltion for button's Onclick event will be created. Add the lines: BasicForm->Visible=false;
PersonForm->Visible=true;

so that the complete function will be:
void __fastcall TBasicForm::BitBtn2Click(TObject *Sender)
{ BasicForm->Visible=false;
PersonForm->Visible = true;
}

In the same way it is possible to create the code for the OnClick event of the button we have placed in the PersonForm. Add the lines:
PersonForm->Visible=false;
BasicForm->Visible=true;

so that the complete function will be:

void __fastcall TPersonForm::BitBtn1Click(TObject *Sender)
{ PersonForm->Visible = false;
BasicForm->Visible = true;
}

What we have already written is not enought to make the buttons work. The object PersonForm, actually is not visible from the form BasicForm and, at the same time the BasicForm is not visible from the PersonForm. To solve this problem add the line:

#include "BasicUnit"
at PersonUnit's beginning, and

#include "PersonUnit"
at BasicUnit's beginning.

That's all. We are now ready to compile and execute the application to verify all we have done since now works correctly. Press F9 to run the program and verify form's buttons work in the right way.





Italia HyperBanner Network