摘要:該文通過具體的實例,介紹了多條件數據查詢的設計思想,提供了用于單表的復合查詢的方法。
關鍵詞:復合查詢;宏代換函數;屬性;表單控件
中圖分類號:TP311文獻標識碼:A 文章編號:1009-3044(2008)32-1123-02
Elementary Research on the Methods of the Construction of Multi-conditional Requirements in VFP
WANG Ya-jun
(Anhui Since and Technology University,computer department, Fengyang 233100, China)
Abstract: The paper is about the ideas for the design of data requirements under multi circumstances through concrete examples. Meanwhile it is also concerned about the methods of the compound requirements applied to a single table.
Key words: compound requirement; macro replacement function; attribute; form activex
1 引言
數據查詢是信息管理的重要操作,實現復雜的數據查詢也是數據處理的難點之一。在Visual FoxPro 6.0(以后簡稱VFP)中,用戶可以使用查詢命令或查詢向導進行數據表的查詢,但使用這種方式用戶必須熟知VFP命令或工作界面才能得到自己所需的信息,這給用戶進行數據查詢操作帶來極大的不便。若對數據表的查詢設計通過表單來實現,由于多條件查詢組合后會有很多種可能,這無疑增加了查詢程序設計的難度。因此,探討復合查詢的實現方法就很有必要了。
2 查詢條件的設計
2.1 查詢條件的構造思想
首先,選擇查詢條件之間的關系是“與”運算還是“或”運算;其次,分別輸入要查詢的數據,根據第一步中確定的查詢條件之間的關系把查詢條件構造成一個字符表達式;最后,在查詢語句中通過宏代換函數把查詢條件中的表達式置換出來,從而查詢出符合條件的記錄。
2.2 表單中控件設置
現有一個student表,結構為student(寢室號(C,4),姓名(C,8),班級(C,10),系部(C,16),政治面貌(C,8)),在表單form1上建立如下表1所列的對象,使用圖1所示操作界面完成對student表的復合查詢。
表單上主要控件名稱、屬性及說明如表1。
2.3程序的實現
Command2的click事件代碼(即查詢按鈕的事件)如下:
Localcxtj1,cxtj2
cxtj1=\".T.\"
if not empty(alltrim(thisform.txtqsh.value))
cxtj1=cxtj1 + \"and '\" + alltrim(thisform.txtqsh.value) + \"' $ 寢室號\"
endif
ifnotempty(alltrim(thisfomr.txtxm.value))
cxtj1=cxtj1 + \" and '\" + alltrim(thisform.txtxm.value) + \"' $ 姓名\"
endif
if .not.empty(alltrim(thisform.txtbj.value))
cxtj1=cxtj1 + \" and ' \" + alltrim(thisform.txtbj.value) + \"' $ 班級\"
endif
IF .not. empty(thisform.combxb.value)
cxtj1=cxtj1 + \" and '\" + alltrim(thisform.combxb.value) + \"' $ 系部\"
endif
ifnot empty(thisform.combzzmm.value)
cxtj1=cxtj1 + \" and '\" + alltrim(thisform.combzzmm.value) + \"' $ 政治面貌\"
endif
cxtj2= \".f.\"
if .not. empty (alltrim(thisformtxtqsh.value))
cxtj2=cxtj2 + \" or '\" + alltrim(thisform.txtqsh.value) + \"' $ 寢室號\"
endif
if . not. empty (alltrim(thisform.txtxm.value))
cxtj2=cxtj2 + \" OR '\" + alltrim(thisform.txtxm.value) + \"' $ 姓名\"
endif
if . not. empty (alltrim(thisform.txtbj.value))
cxtj2=cxtj2 + \" OR '\" + alltrim(thisform.txtbj.value) + \"' $ 班級\"
endif
if . not. empty (thisform.combxb.value)
cxtj2=cxtj2 + \" OR '\" + alltrim(thisform.combxb.value) + \"' $ 系部\"
endif
if . not. empty(thisform.combzzmm.value)
cxtj2=cxtj2 + \" OR '\" + alltrim(thisform.combzzmm.value) + \"' $ 政治面貌\"
endif
if empty(thisform.txtqsh.value) andempty(thisform.txtxm.value) and;
empty(thisform.txtbj.value) andempty(thisform.combxb.value)and;empty(thisform.combzzmm.value)
??chr(7)
=messagebox(\"你沒有輸入查詢值,請重新輸入!\",48,\"信息窗口\")
else
do case
casethisform.option1.Value=1
select* from studentwhere cxtj1. into cursor temp
thisform.grid1.recordsource='temp'
case thisform.option1.Value=2
select* from studentwhere cxtj2. into cursor temp
thisform.grid1.recordsource='temp'
endcase
endif
thisform.refresh
Command2的click事件代碼(即清空按鈕的代碼)如下:
thisform.txtqsh.Value=\"\"
thisform.txtxm.Value=\"\"
thisform.txtbj.Value=\"\"
thisform.txtqsh.Value=\"\"
thisform.combxb.Value=\"\"
thisform.combzzmm.Value=\"\"
thisform.grid1.recordsource=\"\"
thisform.Refresh
3 結束語
通過以上設計,可實現多條件查詢,而且輸入條件為空時它能提示用戶,使用戶輸入的條件更規范化。本設計實現了用戶數據復雜時的查詢,它允許用戶任意組合條件來獲取最簡化的結果,而且使用方便,易于維護。當然本文僅對單表查詢進行說明,若要進行多表查詢操作,還需做進一步的設計。
參考文獻:
[1] 史濟民,湯觀全.VisualFoxpro 6.0及其應用系統開發[M].北京:清華大學出版社,2000.
[2] 韓中孝.Visual FoxPro數據庫系統項目開發實踐[M].北京:科學出版社,2005.
[3] 張洪舉.Visual FoxPro程序設計參考手冊[M].北京:人民郵電出版社,2004.