机房收费系统之组合查询

时间:2022-09-16 10:31:36

     对于机房收费系统,总体来说不太难,比较让人费脑筋的地方也不过上下机,组合查询和结账部分,这三部分是需要我们动脑筋的,在做之前,我们得先熟悉这几部分的作用是什么,它们能实现什么功能,只有我们把其中的逻辑思路理清了,在接下来实际操作完善的时候才会得心应手。组合查询的实现有很多种方式,各有优劣,下面是我实现组合查询的部分代码。以学生上机统计信息窗体为例。

机房收费系统之组合查询

从左到右各个控件的名称为:cboField(0),cboOperator(0),Text1(0),DTP1,cboRelation(0)

cboField(1),cboOperator(1),Text1(1),DTP2,cboRelation(1)

cboField(2),cboOperator(2),Text1(2),DTP3,cboRelation(2)

Form_Load()中添加combobox的加载信息和设置第二和第三行的查询条件不可用

   Dim a, b, c 
For a = 0 To 2
With cboField(a)
.AddItem ""
.AddItem "卡号"
.AddItem "姓名"
.AddItem "上机日期"
.AddItem "上机时间"
.AddItem "下机日期"
.AddItem "下机时间"
.AddItem "消费金额"
.AddItem "余额"
End With
Next a
For b = 0 To 2
With cboOperator(b)
.AddItem ""
.AddItem "="
.AddItem "<>"
.AddItem "<"
.AddItem ">"
End With
Next b
For c = 0 To 1
With cboRelation(c)
.AddItem ""
.AddItem "与"
.AddItem "或"
End With
Next c

cboField(1).Enabled = False
cboOperator(1).Enabled = False
Text1(1).Enabled = False
cboField(2).Enabled = False
cboOperator(2).Enabled = False
Text1(2).Enabled = False
cboRelation(1).Enabled = False

然后定义了一个过程,保证可以从数据库中选择正确的字段

Public Function Fieldname(strFiledname As String) As String

Select Case strFiledname
Case "卡号"
Fieldname = "cardno"
Case "姓名"
Fieldname = "studentName"
Case "上机日期"
Fieldname = "ondate"
Case "上机时间"
Fieldname = "ontime"
Case "下机日期"
Fieldname = "offdate"
Case "下机时间"
Fieldname = "offtime"
Case "消费金额"
Fieldname = "consume"
Case "余额"
Fieldname = "cash"

End Select

End Function




然后就是字段名的click事件,添加了DTP控件选择日期和时间,可以避免输入错误的时间格式。

Private Sub cboField_Click(Index As Integer)
If Trim(cboField(0).Text) = "上机日期" Or Trim(cboField(0).Text = "下机日期") Then
DTP1.Visible = True


ElseIf Trim(cboField(0).Text) = "上机时间" Or Trim(cboField(0).Text) = "下机时间" Then
DTP1.Visible = True
DTP2.Visible = False
DTP3.Visible = False
DTP1.Format = dtpTime
DTP1.Value = Time
End If

If Trim(cboField(1).Text) = "上机日期" Or Trim(cboField(1).Text = "下机日期") Then
DTP2.Visible = True


ElseIf Trim(cboField(1).Text) = "上机时间" Or Trim(cboField(1).Text) = "下机时间" Then
DTP2.Visible = True
DTP1.Visible = False
DTP3.Visible = False
DTP2.Format = dtpTime
DTP2.Value = Time
End If

If Trim(cboField(2).Text) = "上机日期" Or Trim(cboField(2).Text = "下机日期") Then
DTP3.Visible = True

ElseIf Trim(cboField(2).Text) = "上机时间" Or Trim(cboField(2).Text) = "下机时间" Then
DTP3.Visible = True
DTP1.Visible = False
DTP2.Visible = False
DTP3.Format = dtpTime
DTP3.Value = Time
End If

End Sub

判断第一个和第二个的选择关系,有还是没有“与” 或 “或”

Private Sub cboRelation_Click(Index As Integer)
 If Trim(cboRelation(0).Text) <> "" Then       '第一个关系不为空时,第二行可用
        cboField(1).Enabled = True
        cboOperator(1).Enabled = True
        Text1(1).Enabled = True        
        cboRelation(1).Enabled = True     
 End If
     
    If Trim(cboRelation(1).Text) <> "" Then       '第一个关系不为空时,第二行可用
        cboField(2).Enabled = True
        cboOperator(2).Enabled = True
        Text1(2).Enabled = True

  End If
End Sub


接下来就是查询按钮里的东西了

 If Trim(cboField(0).Text) = "" Or Trim(cboOperator(0).Text) = "" Or Trim(Text1(0).Text) = "" Then
            MsgBox "请把您的输入项填写完整", vbOKOnly + vbExclamation, "警告!"
            Exit Sub
    Else
        txtsql = "select * from line_info where " & Fieldname(cboField(0).Text) & cboOperator(0).Text & "'" & Text1(0).Text & "'"
        Set mrc = ExecuteSQL(txtsql, msgtext)
    从数据库中选择信息就采用了上面的办法, 当条件多的时候 方法类似,  


接下来就是理清选择关系,当第一个选择关系不为空时,当第二个选择关系不为空时

然后就是通过DTP控件的选择和失去焦点来让DTP控件给文本框赋值,

Private Sub DTP1_Change()
Text1(0).Text = DTP1.Value

End Sub
Private Sub DTP2_Change()
Text1(1).Text = DTP2.Value

End Sub
Private Sub DTP3_Change()
Text1(2).Text = DTP3.Value

End Sub

Private Sub DTP1_LostFocus()
Text1(0).Text = DTP1.Value
DTP1.Visible = False
End Sub
Private Sub DTP2_LostFocus()
Text1(1).Text = DTP2.Value
DTP2.Visible = False
End Sub
Private Sub DTP3_LostFocus()
Text1(2).Text = DTP3.Value
DTP3.Visible = False
End Sub
这样就会在选择到日期或时间条件时通过DTP控件来得到便利。还有这里用到了数组,可谓是给清空时节省了一大部分代码,这一部分难的是理清选择条件,好好思考下选择条件就会发现也不过如此。