1. 程式人生 > >機房收費系統組合查詢

機房收費系統組合查詢

    
    下面說一下我在組合查詢中的思路:
    1、首先遇到的困難就是‘欄位名’ 和 ‘組合關係’對應的文字和我們要查詢表中的欄位不是一致的(欄位名=學號,對應資料表中 studentNo 但是 如何當我們輸入學號讓它查詢是對應的studentNo中去?是一個問題)
    解:定義一個函式DateView()
 public Function DateView(a as string)as string
  select case a
      case "學號"
          DateView = "studentNo"
      case "姓名"
   DateView = "studentname"
   .
   .
   .
   .
  end select
    這樣就可以使窗體中的字串和資料表中對應欄位一致了
    2、緊接著是查詢語句,對於多條件查詢也就是and / or 的問題。
        比如查詢student表中學號(studentno)在3—9之間的學生可以這樣寫:
 dim txtsql as string
 txtsql="select * from student where studentno>'"&"3"&"' and student<'"&"9"&"'"
    3、向窗體中顯示資料
        使用do while語句一條一條的新增資料
    現在基本程式碼是實現了,但是對於許多能夠出現的錯誤要避免。下面是我在查詢"學生上機統計查詢"的程式碼:
public Function Field(a as string ) as string
 select case a
     case"卡號"
  field="cardno"
     case"姓名"
  field="studentname"
     case"上機日期"
  field="ondate"
     case"上機時間"
  field="ontime"
     case"下機日期"
  field="offdate"
     case"下機時間"
  field="offtime"
     case"消費金額"
  field="consume"
     case"餘額"
  field="cash"
     case"備註"
  field="status"
 end select
End Function
Private Sub CmdInquire_Click()
    Dim Mrc As ADODB.Recordset
    Dim txtSQL As String
    Dim MsgText As String
    '如果第一行輸入內容有空,提示資訊
    If Trim(ComboField1.Text) = "" Or Trim(ComboOperator1.Text) = "" Or Trim(txtTest1.Text) = "" Then
        MsgBox "請輸入完整的查詢條件", , "提示"
        Exit Sub
    End If
   
    txtSQL = "select * from line_Info where  "
    txtSQL = txtSQL & Field(ComboField1.Text) & Trim(ComboOperator1.Text) & "'" & Trim(txtTest1.Text) & "'"
    If Trim(ComboRelation1.Text <> "") Then
        If Trim(ComboField2.Text) = "" Or Trim(ComboOperator2.Text) = "" Or Trim(txtTest2.Text) = "" Then
            MsgBox "您選擇了第一個組合關係,請輸入第二行條件在查詢", vbOKOnly, "提示"
            Exit Sub
        Else
            txtSQL = txtSQL & Field(ComboRelation1.Text) & " " & Field(ComboField2.Text) & ComboOperator2.Text & "'" & Trim(txtTest2.Text) & "'"
        End If
    End If
    If Trim(ComboRelation2.Text) <> "" Then
        If Trim(ComboField3.Text) = "" Or Trim(ComboOperator3.Text) = "" Or Trim(txtTest3.Text) = "" Then
            MsgBox "您選擇了第二個組合關係,請輸入第三行條件在查詢", vbOKOnly, "提示"
            Exit Sub
        Else
            txtSQL = txtSQL & Field(ComboRelation2.Text) & " " & Field(ComboField3.Text) & ComboOperator3.Text & "'" & Trim(txtTest3.Text) & "'"
        End If
    End If