Read Clients from the Database

Navigation:  Tutorial: The Form Editor > Sample creation of a form >

Read Clients from the Database

Version 1.0.0

On the empty form you can now create a label and a ListBox. To do so, click on the respective icon in the toolbox. With another click on the form, the component is created at the position of the mouse. You can now change the properties of the currently selected component with the Object Inspector. For example, the name of the label can be placed on clients and the height of the ListBox can be changed.

 

11.3.1 - Formeditor

Add the necessary elements

 

To ensure that the ListBox contains all clients when the form is called up, the matching program code must be created in Delphi script. Click on the tab above the source code form. The source code for the form is now displayed, where you will find the two functions, OkButtonClick and CancelButtonClick. These functions are intended for the existing OK or Cancel button, with which the form is closed via CloseForm () with a corresponding return value.

 

Now create manually the function FormActivate, which runs automatically when the form is activated. Here you first declare the variables LSQL as a string, LClients as TStringList and i as integer. Then you initialize the LClients variable. These variables are needed to later access the database.

 

Next, you create a SQL command to retrieve the computer names of all clients from the ACMP database and store it in the variable LSQL. To obtain access to the ACMP database, you must first import the AagonSQLquery unit. To do this, open the command reference, select the AagonSQLQuery unit and click on Add.  To be able to run the sqlquery, the following parameters are required:

 

SQLStatement

The actual SQL statement In the LSQL example.

ConnectionString

Connection data for the SQL server in the standard notation. Is not needed in this example, because the query is exectured via the

ExecuteOnACMPServer        parameters on the ACMP server.

Variable

Project variable in which the result can be saved.

ResultList

String list in which the results are saved. In the example, this is the LClients variable.

ExecuteOnACMPServer

A true/false indication of whether the query is to be executed on the ACMP database. When specifying true, no ConnectionString is needed.

 

After running the query, the query results are saved in the LClients variable. By using a for loop, the results are now scanned and added to the ListBox. Since the results in the column name=value form, the column name (in this case, computer name) including the equals sign must first be removed, which is done via the StringReplace function.

 

Procedure FormActivate(Sender: TObject)

var

 LSQL: String;

 LClients: TStringList;

 i: integer;

begin

 LClients := TStringList.Create;

 try

          LSQL := 'Select COMPUTERNAME from CLT_CLIENTS';

          sqlquery(LSQL, '', '', LClients, true);

 

          for i := 0 to LClients.count-1 do

          begin

               ListBox1.items.add(StringReplace(LClients.strings[i], 'COMPUTERNAME=', ''));

          end;

 finally

          LClients.Free;

 end;

end;

 

Finally, the resources of the LClients variable by activating LClients.Free release. Now assign this function to the OnActivate event, so that the function can be executed automatically. When you now start the form, the computer names of all clients are listed in the ListBox. Depending on the number of clients, this may take a few seconds.

Last change on 10.03.2014