Creating a database form
Contents
Overview
With the HBasic IDE you may select a database table and display or edit
it's contents with a table view. To edit the rows of a database table while
executing a HBasic program you may read each data row and put the values
in widgets that you have placed on a form. This works but needs much code
that always looks the same way.
It is more comfortable to set up a database form and connect lineedit widgets
to the columns of a table. You may use the DBFORM widget from the hbasic_dbaccess
package to create this connections. With the DB-Form wizard you can create
a new form that already has a DBFORM component and one label and one lineedit
widget for each column of a given database. When starting this form the label
widget displays the name of the column and the lineedit widget displays the
value of the column.
For the
tab1 table of the hbasic example database with 3 columns named
col1,
col2 and
col3 the generated form looks like the
following image.
[
画像:wizard_db_form]
Prerequisites
Before you may start the database form wizard you have to prepare the
following steps:
- Create a database connection for an existing database (database dialog)
- Connect this database to your project (Button To project in
database dialog)
[
画像:db_in_project]
Select database and connect to project
Since a database form will be stored in the project files of your current
HBasic project you have to select a database and connect it to the
project first. You can select a database in the database window. Open
the database window with the menu entry view / database window or click on
the small database icon in the toolbar.
HBasic will open a new dialog where you can edit the list of known databases
or connect to one database. Remember to compile the database plugin for
your favourite database backend before trying to use the database connection
in HBasic. (See setting up database access with HBasic).
To connect a database to your project add the database description to
the list of known databases on top of the database window and click on
the button
to project to connect the current selected database to
your project description.
Start database form wizard
If you have connected your HBasic project to a database you can open
the DB form wizard by clicking on the
DB-Form wizard icon
which appears when you select
New in the menubar or click on the
new icon in the project tree toolbar or HBasic global toolbar.
[
画像:new_doc]
Select table for your database form
The only additional information the DB Form wizards needs is the name of
the table for which you want to set up the row editor form. After clicking
on the DB Form wizard icon a new dialog pops up which displays all tables
of the database that is connected to your project. If there are no tables
listed in this dialog check your database connection and if the database
is online.
Select one of the tables from this list and click on the OK button. HBasic
will now create a new form in your project and insert all widgets needed
to edit the columns of the database table into this form.
Load DB form from another form
When you start your current HBasic project the new database form will now
be displayed on startup. You have to show it with a special statement within
the start form of your project. Insert for example a new button
button1
into your startform (Currently this is automatically the form with the
name
Form1) and insert the following code to display the database
form
Form2.
Sub button1_clicked()
Form2.load()
End Sub
Example: Load and display form
Form2
When you start this changed HBasic project the startup form Form1 with the
button will be displayed. If you click on the button a second form will pop
up which is your database form. You may now scroll through the rows of your
database data or change it like your normally do it with the DBFORM component.