@Tom_P , I've created a sample script here that uses explicit scope control to keep track of UI elements and provide a more succinct callback mechanism. This is an example script and has a bunch of boiler-plate needed. If you find yourself following this method and making many interactive UI scripts, I advise to create a library to perform the boiler-plate code automatically (JSL has a wonderful ability to use meta-programming to modify scripts before they're run).
The below method allows for persistence of the inputs as well as protection on the callbacks.
/* SQL Query */
self = New Namespace();
self:tool name = "SQL Setup";
Eval( Eval Expr(
self:initialize = Function( {},
self = Namespace( Expr( self << Get Name ) );
If( File Exists( "$TEMP/SQL_Setup.jsl" ),
self:settings = Include( "$TEMP/SQL_Setup.jsl" );
,
self:settings = [=>];
);
self:available companies = {"", "AAA", "BBB", "CCC"};
self:available products = {"", "Prod1", "Prod2"};
1
);
self:content = Function( {},
V List Box(
H List Box(
Panel Box( "Select Program to Run:",
V List Box(
self:DataParam 1 = self:Check Box( {"Option_1"}, "dataparam 1", "single select(this, 1)" )
,
self:DataParam 2 = self:Check Box( {"Option_2"}, "dataparam 2", "single select(this, 2)" )
)
)
,
Panel Box( "Data Filter",
Lineup Box( N Col( 3 ), Spacing( 3 ),
V List Box( Align( "Right" ), Text Box( "Company:" ) )
,
self:company 1 = self:Combo Box( self:available companies, "company 1" )
,
self:company 2 = self:Combo Box( self:available companies, "company 2" )
,
V List Box( Align( "Right" ), Text Box( "Customer:" ) )
,
self:product 1 = self:Combo Box( self:available products, "product 1" )
,
self:product 2 = self:Combo Box( self:available products, "product 2" )
)
)
)
,
H List Box(
self:Button Box( "OK", "run sql" )
,
Spacer Box( <<Set Auto Stretching( 1, 0 ) )
,
self:Button Box( "Cancel" )
)
)
);
self:finalize = Function( {},
self = Namespace( Expr( self << Get Name ) );
self:single select;
1
);
self:single select = Function( {this = 0, index = 0},
{Default Local},
self = Namespace( Expr( self << Get Name ) );
If( {self:DataParam 1, self:DataParam 2} << Get( 1 ) == {1, 1},
If( index == 1,
self:DataParam 2 << Set( 1, 0, Run Script( 1 ) )
,
self:DataParam 1 << Set( 1, 0, Run Script( 1 ) )
)
,
{self:DataParam 1, self:DataParam 2} << Get( 1 ) == {0, 0},
Try( this << Set( 1, 1, Run Script( 1 ) ), self:DataParam 1 << Set( 1, 1, Run Script( 1 ) ) );
);
);
self:button box = Function( {name, callback = 1},
{Default Local},
self = Namespace( Expr( self << Get Name ) );
Eval( Parse( Eval Insert( "\[
box = Button Box( name, <<Set Function(
Function( {this},
{Default Local},
self = Namespace( "^self << Get Name^" );
callback = "^callback^";
If( callback != "1",
Try( Eval( Parse( "self:" || callback ) ), Show( exception_msg ) )
)
)
) )
]\" ) ) );
);
self:combo box = Function( {values, name, callback = 1, default = 1},
{Default Local},
self = Namespace( Expr( self << Get Name ) );
Eval( Parse( Eval Insert( "\[
box = Combo Box( values, <<Set Function(
Function( {this, index},
{Default Local},
self = Namespace( "^self << Get Name^" );
name = "^name^";
callback = "^callback^";
self:settings[name] = this << Get Selected;
If( callback != "1",
Try( Eval( Parse( "self:" || callback ) ), Show( exception_msg ) )
)
)
) )
]\" ) ) );
If( self:settings << Contains( name ),
index = Contains( values, self:settings[name] );
box << Set( index, 0 )
,
box << Set( default, 0 )
);
box
);
self:check box = Function( {values, name, callback = 1, default = {}},
{Default Local},
self = Namespace( Expr( self << Get Name ) );
Eval( Parse( Eval Insert( "\[
box = Check Box( values, <<Set Function(
Function( {this, index},
{Default Local},
self = Namespace( "^self << Get Name^" );
name = "^name^";
callback = "^callback^";
self:settings[name] = this << Get Selected;
If( callback != "1",
Try( Eval( Parse( "self:" || callback ) ), Show( exception_msg ) )
)
)
) )
]\" ) ) );
If( self:settings << Contains( name ),
N = N Items( self:settings[name] );
vals = self:settings[name];
For( i = 1, i <= N, i++,
index = Contains( values, vals[i] );
box << Set( index, 1, 0 )
)
,
N = N Items( default );
For( i = 1, i <= N, i++,
box << Set( i, 1, 0 )
);
);
box
);
self:destroy = Function( {},
self = Namespace( Expr( self << Get Name ) );
Save Text File( "$TEMP/SQL_Setup.jsl", Char( self:settings ) );
1
);
self:run sql = Function( {},
{Default Local},
self = Namespace( Expr( self << Get Name ) );
{company_1, company_2, product_1, product_2} = {self:company 1, self:company 2, self:product 1, self:product 2} << Get Selected;
program_1 = self:DataParam 1 << Get( 1 );
program_2 = self:DataParam 2 << Get( 1 );
code_1 = If( (Company_1 != "") & (Company_2 != ""),
Eval Insert( "AND (COMPANY LIKE '%^Company_1^%' OR COMPANY LIKE '%^Company_2^%))" )
,
(Company_1 != ""),
Eval Insert( "AND COMPANY LIKE '%^Company_1^%')" )
);
code_2 = If( (Product_1 != "") & (Product_2 != ""),
Eval Insert( "AND (PRODUCT LIKE '%^Product_1^%' OR PRODUCT LIKE '%^Product_2^%))" )
,
(Product_1 != ""),
Eval Insert( "AND PRODUCT LIKE '%^Product_1^%')" )
);
Show( code_1, code_2 );
//based on selected checkbox, run the query associated and insert code_1,code_2 at end of sql statement
If(
Program_1 != 0, self:SQL_Query_1( code_1, code_2 )
,
Program_2 != 0, self:SQL_Query_2( code_1, code_2 )
);
);
self:SQL_Query_1 = Function( {code_1, code_2},
{Default Local},
self = Namespace( Expr( self << Get Name ) );
sql_statement_1 = Eval Insert( "Select *
from TABLE.DATA
WHERE SCANDATETIME > (SYSDATE - 30)" + code_1 + code_2 );
Show( sql_statement_1 );
Open Database( "DSN=xxx;UID=xxx;PWD=xxx;", sql_statement_1, "Table1" );
);
) );
Eval( Eval Expr(
self:initialize();
New Window( self:tool name,
<<Modal,
<<On Close(
self = Namespace( Expr( self << Get Name ) );
Try( self:destroy );
self << Delete Namespace();
1
)
,
<<On Open(
self = Namespace( Expr( self << Get Name ) );
self:window = Current Window();
self:finalize();
Print( self << Get Name );
1
)
,
self:content
);
) );
Jordan