When a data window is created using PowerBuilder, you cannot select the data source and display type. How can users define their own data sources and display types in the data window in the application? To achieve this goal, we must dynamically create a data window at the program stage based on your own needs. The following describes how to dynamically create a data window based on actual conditions.
Before creating a dynamic data window, you must first obtain the syntax of the data window object. Pb provides the syntaxfromsql () function for us. You can use this function to obtain the syntax for creating a data window. The basic format of the syntaxfromsql () function is as follows:
Transaction object. syntaxfromsql (Data Source string, display type string, error string)
The following is an example of creating a data window object Syntax:
// Declare Variables
String ls_ SQL, ls_present, ls_err, ls_syntax
// Define the data source of the data window
Ls_ SQL = "select emp_id from employee"
// Define the string displayed in the data window
Ls_present = "style (type = tabular )"
// Obtain the data window object creation syntax.
// If an error occurs, syntaxfromsql returns an empty string
Ls_syntax = sqlca. syntaxfromsql (ls_ SQL, ls_present, ls_err)
The data window object created using syntaxfromsql () is an object created in the running stage and is not automatically stored in the Pb object database library. To reuse this data window object in the future, you can use the libraryimport () function to save the data window object.
After you use the syntaxfromsql () function to create a data window syntax, you must combine it with the data window control on the window. This data window control must already exist in the window. To combine the data window object syntax with an existing data window control, you must use the CREATE () function. The following is an example program:
// Declare the variable string ls_syntax, ls_err
Ls_errdw_1.create (ls_syntax, ls_err)
Dw_1.settransobject (sqlca)
Dw_1.retrieve ()
In the above program, ls_syntax is the string returned after the syntaxfromsql () function is run. In addition, you can use the libraryexport () function to obtain the data window object syntax that already exists in the pbl from the Pb object database. Because the CREATE () function destroys the combination of the previous data window and the transaction object, you must use the setransobject () function to re-combine the transaction object to the new data window object, and then run retrieve () function to read data from the database.
Finally, we need to dynamically create a new data window object to a data window control using the SQL statement string. Although a data window control cannot be directly created in the window during the running stage, a user object can be used to create a data window control that is the same as that on the window, this user object is opened dynamically during the running stage. The following describes how to dynamically create a data window control:
1. Create a user class for the standard data window in the user object canvas.
2. You can add functions, programs, user object functions, and user-defined events to the control and save the user objects to the object database.
3. Use the openuserobject () function to create a replica of a user object in the data window. The general format is as follows: openuserobject (user object variable, {x coordinate value, Y coordinate value }). The following is an example of creating a user object u_dw at the coordinates clicked by the mouse in the window:
OpenUserObject (u_dw, Pointerx (), Pointery ())
The above are the basic steps for creating a dynamic data window. If you are interested, try again.