# FORMS

This version (0.97) of PgAccess has changed the form API : variable handling, query results interface and control bindings naming convention. Please read it carefully, download the database demo and practice a while before trying to design your own forms.

For the moment, it has only some basic widgets : labels, entries, buttons , listboxes , checkboxes and radiobuttons.

Also there is a pseudo data control widget that allows you yo have access to a query results.

How do you generate widgets :

1. select a widget from the toolbox by clicking the appropriate radiobutton
2. move to the canvas , point with the mouse at the desired location and click the mouse button to begin
3. keeping the mouse-button pressed move the mouse in order to draw a rectangle that will hold the widget
4. release the mouse-button

In the rectangle that you have designed it will appear the selected object.
Move now to the attribute window to change some of its properties.

Renaming, resizing items are possible (for the moment) only by modifying appropriate parameters in attribute window. You must press Enter in the edit field after changing a value in order to be accepted.

You can also move items by dragging them or delete them by pressing Del key after selecting them.

In attribute window, there are some fields named `Command` and `Variable`.

The field `Command` have meaning only for Button widgets and holds the command that will be invoked when the button is pressed.

The field `Variable` have meaning only for EditField , Label widgets , checkboxes and radiobuttons and it is the name of the global variable that will hold the value for that widget. For checkboxes the values are t and f (from true and false) in order to simplify binding to logical data fields (PgAccess 0.82 used 0 and 1).

For radiobuttons, it is usual to assign the same variable to the same radiobuttons within the same group. That variable will contain the name of the widget of the radiobutton that has been pressed. Let's presume that you have entered 3 radiobuttons named red, green and blue, all of them having the same variable named color. If you will press them, they will assign their names to global variable.

In order to make a simple test, put an entry field and set it's variable to v1 and a button who's command is "set v1 whisky". Press the button "Test form" and click on the button. In that entry should appear whisky.
Another test is defining in Script module a script called "My first script" having the following commands:
`tk_messageBox -title Warning -message "This is my first message!"`
and then define a button who's command is `execute_script "My first script"`.

## Database manipulation

Let's presume that our form have the internal name `mf` (my form). Don't forget that the Tk window names could not start with an uppercase letter.
The window will be referred inside the Tcl/Tk source as `.mf`
If you want to close the form in run-time you have to issue the command `destroy .mf`

Also, any widget created inside this window (form) will have the name prefixed by `.mf ,`so we will have `.mf.button1` or `.mf.listbox1` .

We can name the data control widget `dc` for example. The fully qualified name for that "virtual widget" will be `.mf.dc` then. A new namespace called `DataControl(.mf.dc)` will be automatically defined.
The `Command` property of the data control widget must contain the SQL command that will be executed.
When the form will be in run-time, automatically you will have access to the following procedures and functions from the namespace:

`open` - opens the connection and execute the query (returns nothing)
`setSQL newsql` - set the command query that will be executed at the next `open`
`getRowCount` - returns the number of records of the result set
`getRowIndex` - returns the current record number inside the result set
`getFieldList` - returns a Tcl list containing the fields names from the current result set
`moveFirst` - move the cursor to the first record in the recordset
`moveLast``, moveNext , movePrevious`- moves the cursor there
`moveTo newrecno` - move the cursor to that new record number (first is 0)
`updateDataSet` - update the variables inside the designed form that have a particular name (I'll explain later)
`clearDataSet` - clear the associated DataSet variables
`fill listbox field` - fill the named listbox (whole widget name as `.mf.listbox1`) with the all the values of that field from the current result set
`close` - close the result set (if you don't close it, you will loose some memory)

These procedures and functions should be called in the normal Tcl namespace mode as in the following example:

`DataControl(.mf.dc)::setSQL "select * from phonebook"`
`DataControl(.mf.dc)::open`
`set nrecs [DataControl(.mf.dc)::getRowCount]`

If you complaint about writting to many DataControl(...) you can include many commands into a single namespace eval as in the following example :

`namespace eval DataControl(.mf.dc) {`
`setSQL "select * from phonebook"`
`open`
`set nrecs [getRowCount]`
`moveLast`
`updateDataSet`
`}`

It's no need to close a query-result set if you want to assign it a new SQL command and open it again. That will be done automatically releasing the memory used for the last result set.
Opening a new DataControl will automatically position the current row index of the result set on the first row (index 0) and will define a new global associative array named DataSet that will hold data from the current row. The key into that array will be the fully qualified name of the data control widget followed by a comma and the name of every field in the selected rows.

Example:
`DataSet(.mf.dc,name)`
`DataSet(.mf.dc,city)`

If you want to bound some controls to the fields of the recordset, you will have to name their associate variable like that :

`DataSet(.mf.dc,salary)` to get the "salary" field , or `DataSet(.mf.dc,name)` to get the "name" field. Using the data control procedures `DataControl(.mf.dc)::moveNext` or movePrevious will automatically update the `DataSet(.mf.dc,...)` array so the database information from entries in the form will be refreshed.

Here it is a dumped sample database that contains a demo database. What should you do ?
Shift-click the above URL in order to download that tiny file (4 Kb). Create a empty database and `psql yourdatabase <formdemo.sql`

You should find a single table called "phonebook" a form called "Phone book" and another "A simple demo form".

First of all enter and view the phonebook table in table view. Note the fields and their values.
Open the "Phone book" form and enter a letter (a, e or i) in the field to the left of "Find" button then press Find. It's fine to enter one letter in order to get more records in query result. You will get information about the number of records selected, in the listbox you will see all the values of field "name" from the current data set. Use buttons to move to first, next, previous or last record within the record set.

In order to add a new record, press the "New" button in order to get new, clean entries. Fill them with your data and press "Add new" button. A new phonebook record will be added. Also, if you want to update a record, change it's values in the displayed fields after finding it and press "Update" button. The values will be updated in the database BUT NOT IN THE CURRENT QUERY RESULT . If you want to see them modified, make a new query trying to find it again.

Before using the results from a query you should know that the information that has been retrieved could be found only in your computer client memory. It has no live connection to the data from the database. That's why it isn't possible to develop a simple update function as interface to that query-result widget. More than that : a query result could be obtained from a SQL command that return a non-updatable data set !!! For example fields gathered from multiple tables or summary fields. It isn't just simple to make an automatic update procedure. The programmer must know how to make the update or the append procedure, sometimes using key fields to point to the desired record or an OID. There are examples in the demo database in "Phone book" form. It may be possible that in the future, I will develop another pseudo-widget describing a table. It would be more simple than to implement an update or append or even a delete procedure.

There is in the demo database also another simple form called "A simple demo form". It will show you how to handle variables from checkboxes, radiobuttons, how to use listboxes, open another forms and so on. I think they will help you.

In order to avoid naming user defined forms with a particular name of another PgAccess form, I would recommend naming them as udf0, udf1 (user defined form 0 , 1 )

Please feel free to send me your opinion at teo@flex.ro on forms designing and usage.

KEEP IN MIND ! THE FORM API MAY CHANGE IN ORDER TO BE MORE SIMPLE AND BETTER!
SEND ME YOUR WISHES, YOUR IDEAS, YOUR OPINIONS !
ALSO ... DON'T BLAME ME IF YOU WILL HAVE TO RE-DESIGN YOUR OLD FORMS DUE TO SOME INCOMPATIBILITIES WITH NEWER PGACCESS VERSIONS.