關於組合查詢,真的是乙個令人頭疼的東西,但是當自己突然間的做出來時,卻莫名的有種貌似又不是很難得感覺。昨天弄了整整一下午,今天的下午終於在除錯了兩個小時做出來了。
首先是查到了一些關於組合查詢的部落格,有乙個共同的特點就是都要獲得使用者所選的欄位名並轉化成資料庫表中的欄位名,這樣實現了人機共同語言的轉換。但是**實在是太複雜了,在小星星的引導下,我知道了可以定義模組的辦法。最後我是採用了這樣一種方法終於成功了。
定義在模組中的**如下:
'將選擇的字段漢字轉化成資料庫中的字段,——計算機語言
'因為以後還會有好多個組合查詢的窗體,會用到下面的字段,一起都寫出來,很方便呼叫
public function fieldname(strfieldname as string) as string
select case strfieldname
case "卡號"
fieldname = "cardno"
case "學號"
fieldname = "studentno"
case "姓名"
fieldname = "studentname"
case "性別"
fieldname = "***"
case "系別"
fieldname = "department"
case "年級"
fieldname = "grade"
case "班號"
fieldname = "class"
case "上機日期"
fieldname = "ondate"
case "上機時間"
fieldname = "ontime"
case "下機日期"
fieldname = "offdate"
case "下機時間"
fieldname = "offtime"
case "消費金額"
fieldname = "consume"
case "餘額"
fieldname = "cash"
case "備註"
fieldname = "status"
case "教師"
fieldname = "level"
case "註冊日期"
fieldname = "logindate"
case "註冊時間"
fieldname = "logintime"
case "登出日期"
fieldname = "logoutdate"
case "登出時間"
fieldname = "logouttime"
case "機器名"
fieldname = "computer"
end select
end function
'將操作符轉化成資料庫表中的欄位名
public function operate(stroperate as string)
select case stroperate
case "="
operate = "="
case "<"
operate = "<"
case ">"
operate = ">"
case "<>"
operate = "<>"
end select
end function
'將連線符轉化成資料庫表中的欄位名
public function connect(strconnect as string) as string
select case strconnect
case "或"
connect = "or"
case "與"
connect = "and"
case ""
connect = ""
end select
end function
學生基本資訊維護窗體
private sub cmdinquire_click()
dim objrs as adodb.recordset
dim txtsql as string
dim msgtext as string
dim stra as string '定義三個儲存sql語句的字串
dim strb as string
dim strc as string
'先寫上乙個不完整的sql語句,等用到時再補充
txtsql = "select * from student_info where "
'每個字串代表分別是三行三種查詢條件時的三種情況
stra = fieldname(cmbfield(0).text) & operate(cmboperate(0).text) & trim(txtcontent(0).text)
strb = stra & " " & connect(cmbconnect(0).text) & " " & fieldname(cmbfield(1).text) & operate(cmboperate(1).text) & trim(txtcontent(1).text)
strc = strb & " " & connect(cmbconnect(1).text) & " " & fieldname(cmbfield(2).text) & operate(cmboperate(2).text) & trim(txtcontent(2).text)
'判斷查詢條件,當只有一行條件時,預設選擇第一行的查詢條件,即第乙個組合關係沒有選擇;
'當有兩條查詢條件時,預設是選擇前兩行,即第乙個組合關係不為空
'當有三行查詢條件時,即每個控制項都不為空,即第二個組合關係不為空
if trim(cmbconnect(0).text) = "" then
if trim(cmbfield(0).text) = "" or trim(cmboperate(0).text) = "" or (txtcontent(0).text) = "" then
msgbox "請在第一行輸入查詢條件", 48, "提示"
exit sub
else
txtsql = txtsql & stra
end if
else
if trim(cmbconnect(1).text) = "" then
if trim(cmbfield(1).text) = "" or trim(cmboperate(1).text) = "" or (txtcontent(1).text) = "" then
msgbox "請在第二行輸入查詢條件", 48, "提示"
exit sub
else
txtsql = txtsql & strb
end if
else
if trim(cmbfield(2).text) = "" or trim(cmboperate(2).text) = "" or (txtcontent(2).text) = "" then
msgbox "請在第三行輸入查詢條件", 48, "提示"
exit sub
else
txtsql = txtsql & strc
end if
end if
end if
set objrs = executesql(txtsql, msgtext)
if objrs.eof then
msgbox "沒有查到記錄", 48, "提示"
exit sub
end if
'將查詢到的資料寫到myflexgrid**中,在寫入資料之前要注意清空資料表,防止資料重複
with myflexgrid
.clear
.cols = 7
.cellalignment = 4
.textmatrix(0, 0) = "卡號"
.textmatrix(0, 1) = "學號"
.textmatrix(0, 2) = "姓名"
.textmatrix(0, 3) = "性別"
.textmatrix(0, 4) = "系別"
.textmatrix(0, 5) = "年級"
.textmatrix(0, 6) = "班級"
.rows = 1
do while not objrs.eof
.rows = .rows + 1
.cellalignment = 4
.textmatrix(.rows - 1, 0) = objrs!cardno
.textmatrix(.rows - 1, 1) = objrs!studentno
.textmatrix(.rows - 1, 2) = objrs!studentname
.textmatrix(.rows - 1, 3) = objrs!***
.textmatrix(.rows - 1, 4) = objrs!department
.textmatrix(.rows - 1, 5) = objrs!grade
.textmatrix(.rows - 1, 6) = objrs!class
objrs.movenext
loop
end with
objrs.close
end sub
機房收費系統 組合查詢
機房收費系統在磕磕絆絆中過來了,這期間遇到問題,解決問題,最後收穫的特別多,在敲得過程中,不斷的學習新知識,應該說組合查詢是收費系統的乙個小難點了吧,起初我是真的不知道該從 下手,總是有種剪不斷理還亂的感覺,分析分析就繞進去了,我總是把問題想的很複雜,其實只要一句 語句,一切都解決了,根本用不到好多...
機房收費系統 組合查詢
組合查詢真的讓我研究了很長時間,從開始沒有什麼思路到閱讀大量的部落格,之後形成自己的 之後一步步的優化,終於成就了乙個自己認為還可以的 若有好的建議歡迎指出,下面是要實現的功能介面 定義變數 dim txtsql as string dim msgtext as string dim mrc as ...
機房收費系統 組合查詢
細節優化 查詢語句 txtsql select from line info where 判斷第一行資訊是否填寫完整 if trim combo1 0 text or trim combo2 0 text or trim a then msgbox 請填寫第一行資訊 vbokonly,提示 end ...