本例對看執行個體學VFP:更複雜的查詢一文做了進一步的改進,在滿足查詢功能的同時,還可以對查詢結果進行統計計算。本例也應用到了資料環境,並使用“資料1”資料庫中的“人員資訊表”作為資料環境的資料來源,關於該資料庫的情況已經在看執行個體學VFP:樣本資料庫一文中給出,運行介面見本文末尾。
製作步驟如下:
一、建立表單,將其caption屬性值設定為“對查詢結果進行統計計算”,AutoCenter屬性值設定為.T.,width屬性值設定為520,height屬性值設定為295,並儲存為“對查詢結果進行統計計算.scx”。
二、右擊表單空白處,選“資料環境”命令,將“人員資訊表”添加到資料環境中。在“資料環境設計器”中拖動“人員資訊表”的標題列到表單上,自動產生一個表格控制項。由於是從資料環境中把“人員資訊表”拖動過來由系統自動產生的,該表格控制項的RecordSource屬性及RecordSourceType屬性已經由系統設定好了,為了書寫代碼的方便,我們把這個表格控制項的name屬性值修改為“grid1”。
三、在表格控制項的下方添加一個Label控制項,並把它的caption屬性值設定為“查詢條件”。
四、在label控制項的右側依次添加兩個下拉式方塊控制項、一個文字框控制項及兩個命令按鈕command1和command2,把這兩個命令按鈕的caption屬性值依次設定為“統計”和“退出”,並把這些控制項排成一行。
五、在命令按鈕控制項這一行的下方添加三個label控制項和三個文字框控制項,並把三個label控制項的caption屬性值依次設定為“合格人數”、“總工資”和“平均工資”,把這些控制項排成一行並把label控制項和文字框控制項依次交叉開。
七、適當調整各控制項在表單上的位置,調整後的表單設計器如下圖所示:
八、設定下拉式方塊控制項的屬性:
(一)組合框Combo1:RecordSourceType屬性值設定為“8-結構”,RecordSource屬性值設定為“人員資訊表”。
(二)組合框Combo2:RecordSourceType屬性值設定為“1-值”,RecordSource屬性值設定為“,>,<,=,>=,<=,<>”。
九、添加事件代碼:
(一)表單的init事件代碼:
set talk off
thisform.text2.value=reccount()
sum(基本工資) to thisform.text3.value
aver(基本工資) to thisform.text4.value
(二)表單的unload事件代碼:close data
(三)組合框Combo1的InteractiveChange事件代碼:
if alltrim(this.displayvalue)="出生日期" thisform.text1.value={}else thisform.text1.value=''endif
(四)“統計”按鈕command1的click事件代碼:
if thisform.grid1.recordsource='臨時人員資訊表' thisform.grid1.recordsource='人員資訊表'endifprivate CXTJif empty(thisform.combo1.displayvalue) or empty(thisform.combo2.displayvalue) ; or empty(thisform.text1.value) && 判斷列表框和文字框是否為空白 messagebox('請輸入完整條件!',16,'系統提示') thisform.combo1.setfocuselse do case case alltrim(thisform.Combo1.Displayvalue)='基本工資'CXTJ=alltrim(thisform.combo1.displayvalue)+' ';+alltrim(thisform.combo2.displayvalue);+' '+alltrim(thisform.text1.value) case alltrim(thisform.Combo1.Displayvalue)='出生日期'CXTJ=alltrim(thisform.combo1.displayvalue)+' ';+alltrim(thisform.combo2.displayvalue);+' ctod("'+dtoc(thisform.text1.value)+'")' otherwiseCXTJ=alltrim(thisform.combo1.displayvalue)+' ';+alltrim(thisform.combo2.displayvalue);+' "'+alltrim(thisform.text1.value)+'"' endcase Select * from 人員資訊表 where &CXTJ. into cursor 臨時人員資訊表 thisform.grid1.recordsource='臨時人員資訊表' thisform.grid1.backcolor=rgb(200,224,248) sele 臨時人員資訊表 thisform.text2.value=reccount() sum(基本工資) to thisform.text3.value aver(基本工資) to thisform.text4.valueendif
(五)“退出”按鈕command2的click事件代碼:thisform.release
十、運行“對查詢結果進行統計計算.scx”,介面見下圖:
參考資料:
vfp基礎教程:http://bianceng.cnhttp://www.bianceng.cn/vfpjc/index0.htm
vfp初級教程:http://bianceng.cnhttp://www.bianceng.cn/cc/index.htm
vfp中級教程:http://bianceng.cnhttp://www.bianceng.cn/mcc/mcc.htm
vfp進階教程:http://bianceng.cnhttp://www.bianceng.cn/hcc/hcc.htm
VFP網路開發:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpwlkf.htm
vfp調用api函數:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpapi.htm
VFP報表列印:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpreport.htm
VFP常用技術:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpcyjs.htm
VFP經驗匯總:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpjyhz.htm
VFP控制項使用:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpkjsy.htm
VFP資料處理:http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpsjcl.htm
本例代碼在Win2003+VFP6.0環境下調試通過。
查看全套“菜鳥也學VFP”教程