機房收費系統 組合查詢

2021-06-11 07:06:08 字數 3939 閱讀 6943

機房收費系統在磕磕絆絆中過來了,這期間遇到問題,解決問題,最後收穫的特別多,在敲得過程中,不斷的學習新知識,應該說組合查詢是收費系統的乙個小難點了吧,起初我是真的不知道該從**下手,總是有種剪不斷理還亂的感覺,分析分析就繞進去了,我總是把問題想的很複雜,其實只要一句sql語句,一切都解決了,根本用不到好多好多的if,for 什麼的,下面是我自己在實現此過程所需要的**:以收費系統中基本資訊維護為例子,其實其他的也都大同小異

只要一句正確的sql,其他全ok!

剛著手時看到這個確實有點迷糊,該從何下手呢?理清楚了才明白,其實很簡單,在這裡我是用組合關係為點,進行判斷,如果組合關係中什麼都沒有選擇,那麼就認為查詢條件為第一行,而且必須選擇完全,如果選擇了組合關係,那麼相對應的2或3行條件就需要填充完整,接著就是按照條件在資料庫中查詢內容了。而我的欄位名選擇是漢字,要想在資料庫中查詢字段,根本就不認識,這就需要我將漢字轉換為相應的字串,**中的fieldname 就是我要利用到的乙個函式

private sub cmdinquire_click()

dim mrc as adodb.recordset

dim txtsql as string

dim msgtext as string

txtsql = "select * from student_info where "

'判斷欄位的選擇是否為空

if not testtxt(combofiledname1.text) then

msgbox "請選擇字段!", vbokonly + vbexclamation, "警告"

combofiledname1.setfocus

exit sub

end if

'判斷操作符的選擇是否為空

if not testtxt(combooperate1.text) then

msgbox "請選擇操作符!", vbokonly + vbexclamation, "警告"

combooperate1.setfocus

exit sub

end if

'判斷要查詢的內容是否為空

if not testtxt(txtcontent1.text) then

msgbox "請在輸入要查詢的內容", vbokonly + vbexclamation, "警告"

txtcontent1.setfocus

exit sub

end if

txtsql = txtsql & filedname(combofiledname1.text) & " " & combooperate1.text & "'" & txtcontent1.text & "'"

'利用模版函式檢視是否是組合查詢第一行為空時,查詢無效

'開始組合查詢

if trim(comborelation1.text <> "") then

if trim(combofiledname2.text) = "" or trim(combooperate2.text) = "" or trim(txtcontent2.text) = "" then

msgbox "您選擇了組合關係,請輸入資料之後再查詢", vbokonly, "提示資訊"

exit sub

else

txtsql = txtsql & filedname(comborelation1.text) & " " & filedname(combofiledname2.text) & combooperate2.text & "'" & trim(txtcontent2.text) & "'"

end if

end if

if trim(comborelation2.text) <> "" then

if trim(combofiledname3.text) = "" or trim(combooperate3.text) = "" or trim(txtcontent3.text) = "" then

msgbox "您選擇了第二個組合,請輸入資料之後在查詢", vbokonly, "提示"

exit sub

else

txtsql = txtsql & filedname(comborelation2.text) & " " & filedname(combofiledname3.text) & combooperate3.text & "'" & trim(txtcontent3.text) & "'"

end if

end if

'開始進行查詢

set mrc = executesql(txtsql, msgtext)

if mrc.recordcount = 0 then

msgbox "沒有您要查詢的學生上機記錄!", vbokonly + vbcritical, "查詢提示"

combofiledname1.setfocus

mshflexgrid1.rows = 1

'存在上機記錄查詢成功,填充到**

else

mshflexgrid1.rows = 1

do while not mrc.eof

with mshflexgrid1

.cellalignment = 4

.rows = .rows + 1

.textmatrix(.rows - 1, 0) = mrc!studentno

.textmatrix(.rows - 1, 1) = mrc!studentname

.textmatrix(.rows - 1, 2) = mrc!cardno

.textmatrix(.rows - 1, 3) = mrc!cash

.textmatrix(.rows - 1, 4) = mrc!department

.textmatrix(.rows - 1, 5) = mrc!grade

.textmatrix(.rows - 1, 6) = mrc!class

.textmatrix(.rows - 1, 7) = mrc!***

.textmatrix(.rows - 1, 8) = mrc!status

.textmatrix(.rows - 1, 9) = mrc!explain

end with

mrc.movenext

loop

end if

exit sub

end sub

'將漢字轉換為相應的欄位名字

public function filedname(strfiledname as string) as string

select case strfiledname

case "卡號"

filedname = "cardno"

case "學號"

filedname = "studentno"

case "姓名"

filedname = "studentname"

case "性別"

filedname = "***"

case "學院"

filedname = "department"

case "年級"

filedname = "grade"

case "班級"

filedname = "class"

case "與"

filedname = "and"

case "或"

filedname = "or"

end select

end function

這樣乙個組合查詢完成了,重其中我也收穫了財富,遇到錯誤的時候就是進步的時候,實現這個功能最大的感慨就是sql語句真的很強大,而我還處在最低階,以後還要努力加油了!

機房收費系統 組合查詢

關於組合查詢,真的是乙個令人頭疼的東西,但是當自己突然間的做出來時,卻莫名的有種貌似又不是很難得感覺。昨天弄了整整一下午,今天的下午終於在除錯了兩個小時做出來了。首先是查到了一些關於組合查詢的部落格,有乙個共同的特點就是都要獲得使用者所選的欄位名並轉化成資料庫表中的欄位名,這樣實現了人機共同語言的轉...

機房收費系統 組合查詢

組合查詢真的讓我研究了很長時間,從開始沒有什麼思路到閱讀大量的部落格,之後形成自己的 之後一步步的優化,終於成就了乙個自己認為還可以的 若有好的建議歡迎指出,下面是要實現的功能介面 定義變數 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 ...