一、建一个外部数据源的dataobject对象,取名为:d_tabular_query ,各列如下:
Name Type Lenth Dec column_name string 10 compare string 3 value string 255 and_or string 3
二、建一窗口w_condition_query_template,Title设置为:“组合条件查询窗口模板”,并加入控件:
控件名 类型 说明 dw_query_condition datawindow 用以输入查询条件 Datawindow Object Name 为前所建数据窗口对象 d_tabular_query cb_add command button 增加一查询条件 cb_del command button 删除一查询条件 cb_ins command button 插入一查询条件 cb_clear command button 清除所有询条件 cb_ok command button 由查询条件得出结果 dw_query datawindow 查询结果 Datawindow Object Name 为空,继承后的子窗口再加入。 cb_all command button 显示所有记录 cb_print command button 打印查询结果 cb_exit command button 退出
三、编写脚本
1、在dw_query_condition的Constractor事件中加入如下脚本:
//-------------------------------------------------------------//
// 功能说明:根据数据窗口dw_query设定可组合项,供组合查询选择
// 注意事项:数据窗口dw_query的对象中text命名须用默认值,
// 即:列名+ _t
// 柯建勋 于1999年4月27日
//-----------------------------------------------------------------------//
long ll_column_count
long ll_i
long ll_row
string ls_column
string ls_column_name
this.settransobject(sqlca)
ll_row = this.insertrow(0)
this.setitem(ll_row,'compare','=')
this.setitem(ll_row,'and_or','and')
ll_column_count = long(dw_query.Describe("DataWindow.Column.Count")) //总列数
this.ClearValues("column_name")
for ll_i = 1 to ll_column_count
if dw_query.Describe("#"+string(ll_i)+".Visible") = '1' then
ls_column = dw_query.Describe("#"+string(ll_i)+".Name") //列名
ls_column_name = dw_query.Describe(ls_column + "_t.text") //列名对应文本
this.SetValue("column_name", ll_i , ls_column_name + "~t" + ls_column)
end if
next
2、定义dw_query_condition的用户事件ue_Enterkeydown,Event ID选择pbm_dwnProcessEnter。
当我们按下Enter键时,将触发此事件。在ue_Enterkeydown事件中加入如下脚本:
//回车键(Enter)切换列焦点
long ll_column_count
long ll_column
ll_column_count = long(this.Describe("DataWindow.Column.Count"))
ll_column = this.getcolumn()
if ll_column = ll_column_count then
cb_add.triggerevent(clicked!) //增加一行
else
this.setcolumn(ll_column + 1)
end if
3、在cb_add的Clicked事件加入:
long ll_row
ll_row = dw_query_condition.insertrow(0)
dw_query_condition.setitem(ll_row,'compare','=')
dw_query_condition.setitem(ll_row,'and_or','and')
dw_query_condition.SetColumn(1)
4、在cb_del的Clicked事件加入:
long ll_row
ll_row = dw_query_condition.getrow()
dw_query_condition.DeleteRow(ll_row)
5、在cb_ins的Clicked事件加入:
long ll_row
long ll_new_row
ll_row = dw_query_condition.getrow()
ll_new_row = dw_query_condition.InsertRow(ll_row)
dw_query_condition.setitem(ll_new_row,'compare','=')
dw_query_condition.setitem(ll_new_row,'and_or','and')
dw_query_condition.SetColumn(1)
6、在cb_clear的Clicked事件加入:
long ll_row
dw_query_condition.reset()
ll_row = dw_query_condition.insertrow(0)
dw_query_condition.setitem(ll_row,'compare','=')
dw_query_condition.setitem(ll_row,'and_or','and')
7、在cb_ok的Clicked事件加入:
//-----------------------------------------------------------------------//
// 条件组合查询 柯建勋 1999.04.27
//-----------------------------------------------------------------------//
long ll_rowcount
long ll_row
long ll_j
string ls_filter_condition
string ls_column
string ls_compare
string ls_value
string ls_and_or
string ls_column_type
if dw_query_condition.AcceptText() = -1 then return
ll_rowcount = dw_query_condition.rowcount()
if ll_rowcount <=0 then return
ls_filter_condition = ''
for ll_row = 1 to ll_rowcount
ls_column = dw_query_condition.getitemstring(ll_row, 'column_name')
ls_compare = dw_query_condition.getitemstring(ll_row, 'compare')
ls_value = dw_query_condition.getitemstring(ll_row, 'value')
ls_and_or = dw_query_condition.getitemstring(ll_row, 'and_or')
ls_column_type = dw_query.Describe(ls_column + ".ColType") //
ls_column_type = left(ls_column_type, 3)
if ll_row = ll_rowcount then //最后一行(不加and、or)
choose case ls_column_type //根据数据类型设置Filter条件
case 'cha' ,'var' ,'str' // 字符型
ls_filter_condition = ls_filter_condition + &
ls_column + ls_compare + "'" + ls_value + "'"
case 'num','dec','lon','rea','ulo' // 数值型
ls_filter_condition = ls_filter_condition + &
ls_column + ls_compare + ls_value
case 'dat','tim' //日期型
ls_filter_condition = ls_filter_condition + "string(" + &
ls_column + ")" + ls_compare + "'" + ls_value + "'"
case else
end choose
else
choose case ls_column_type //根据数据类型设置Filter条件
case 'cha','var','str' // 字符型
ls_filter_condition = ls_filter_condition + &
ls_column + ls_compare + "'" + ls_value + "' " &
+ ls_and_or + " "
case 'num','dec','lon','rea','ulo' // 数值型
ls_filter_condition = ls_filter_condition + &
ls_column + ls_compare + ls_value + " " &
+ ls_and_or + " "
case 'dat','tim' //日期型
ls_filter_condition = ls_filter_condition + "string(" + & ls_column + ")" + ls_compare+ "" + ls_value+ "' " &
+ ls_and_or + " "
case else
end choose
end if
next
dw_query.setredraw(false)
dw_query.setFilter(ls_filter_condition)
if dw_query.filter() = 1 and dw_query.rowcount() > 0 then
ls_column = dw_query_condition.getitemstring(1, 'column_name')
dw_query.setsort(ls_column + ' A')
dw_query.sort()
dw_query.SetColumn(ls_column)
dw_query.selectrow(0,false)
dw_query.selectrow(1,true)
dw_query.setrow(1)
end if
dw_query.setredraw(true)
8、在dw_query的Constractor事件中加入如下脚本:
this.Object.DataWindow.ReadOnly="Yes"
this.settransobject(sqlca)
this.retrieve()
if this.rowcount() > 0 then
this.selectrow(0,false)
this.selectrow(1,true)
this.setrow(1)
end if
9、在cb_all的Clicked事件加入:
dw_query.SetFilter('')
dw_query.Filter()
if dw_query.rowcount() > 0 then
dw_query.selectrow(0,false)
dw_query.selectrow(1,true)
dw_query.setrow(1)
end if
dw_query.SetFocus()
10、在cb_print的Clicked事件加入:
if messagebox("提示信息","确认打印?", question!,Yesno!,1) = 1 then
if PrintSetup () = -1 then
messagebox('出错信息','打印机设置出错!',Exclamation!)
return else dw_query.Print() end if
end if
11、在cb_exit的Clicked事件加入:
close(parent)
本文地址:http://com.8s8s.com/it/it21646.htm