开始敲窗体就从简单的开始敲的,添加,删除,修改,查询,都是按着学生敲的,然后组合查询,学生里边也有组合查询,看看代码,明白一下,又点点给的机房系统,理了理就开始敲,前提是真的思路清了,要不然会感觉好难的,清了就容易了。
组合查询有三个就拿一个学生上机统计信息查询来说。
注意几个方面:
1.字段名不同对应的操作符不同,当字段名为姓名时操作符只有“=”“<>”。其余的是四个。
2.字段名不同对应的查询内容不同,当字段名为上机日期或下机日期时对应的是日期格式,我们在这都使用的日历控件,学生用过,知道怎么整的。当字段名为上机时间或下机时间时格式是时间格式,我们都使用的时间控件。使用时间控件的时候问大姐,大姐说了句“远在天边,近在眼前。”其实是用的我们金龙师父的话。都近在眼前了就自己找呗。看下边:
其实添加的这个控件现在是个日期的,需要改一下它的属性,Format属性改成—2的。就好了。
3.字段名不同对应的查询内容限制不一样,当为时间或日期时文本框是被锁住的。其他的将不锁可添加,删除,修改。
4.限制:当第一行和第二行都为满时第一个组合必须选,反过来但第一行满是选了第一个组合关系,第二行就必须选。一二行都满时的情况跟前边的都是一样的。
我的代码:
Dim txtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
Dim full(3) As Boolean '定义布尔型full数组,代表全有值,一行中。
Dim empt(3) As Boolean '定义布尔型empt数组,代表全没值,一行中。
Dim str1(3) '字段名对应数据库中表的列名。
Dim str2(3) '与对应and ,或对应or
MSHFlexGrid1.Clear
If cmbColName1(0).Text = "注册时间" Or cmbColName1(0).Text = "注销时间" Then
txtContent1(0).Text = DTPicker1.hour & ":" & DTPicker1.Minute & ":" & DTPicker1.Second
End If
If cmbColName1(1).Text = "注册时间" Or cmbColName1(1).Text = "注销时间" Then
txtContent1(1).Text = DTPicker2.hour & ":" & DTPicker2.Minute & ":" & DTPicker2.Second
End If
If cmbColName1(2).Text = "注册时间" Or cmbColName1(2).Text = "注销时间" Then
txtContent1(2).Text = DTPicker3.hour & ":" & DTPicker3.Minute & ":" & DTPicker3.Second
End If
If cmbColName1(0).Text = "" Then '第一条的第一个必须有值。
MsgBox "请选择字段名!", vbOKOnly
cmbColName0.SetFocus
Exit Sub
End If
For i = 0 To 2
If cmbColName1(i).Text = "" And cmbSymbol1(i).Text = "" And txtContent1(i).Text = "" Then
empt(i) = True '三条查询记录中,全是空的empt为True
Else
empt(i) = False '不全为空 为False,包括全有值。
End If
If cmbColName1(i).Text <> "" And cmbSymbol1(i).Text <> "" And txtContent1(i).Text <> "" Then
full(i) = True '三条查询记录中,全有值的full为True
Else
full(i) = False '不全有值为False,包括全是空。
End If
If (empt(i) = False And full(i) = False) Then '当三个中,有的是空,有的有值得情况下。
If cmbColName1(i).Text = "" Then '哪个是空再添加哪个。
MsgBox "请选择字段名!", vbOKOnly
cmbColName1(i).SetFocus
Exit Sub
End If
If cmbSymbol1(i).Text = "" Then
MsgBox "请选择操作符!", vbOKOnly
cmbSymbol1(i).SetFocus
Exit Sub
End If
If txtContent1(i).Text = "" Then
MsgBox "请输入要查询的内容!", vbOKOnly
txtContent1(i).SetFocus
Exit Sub
End If
End If
Next i
If (full(0) = True And full(1) = True) Then '当第一条和第二条都填满时,要添加第一个组合关系。
If cmbnexus1.Text = "" Then
MsgBox "请选择组合关系!", vbOKOnly
cmbnexus1.SetFocus
Exit Sub
End If
End If
If (full(0) = True And full(1) = True And full(2) = True) Then '都有值时,选择组合关系。
If cmbnexus2.Text = "" Then
MsgBox "请选择组合关系!", vbOKOnly
cmbnexus2.SetFocus
Exit Sub
End If
End If
Dim k As Integer
For k = 0 To 2
Select Case cmbColName1(k).Text '字段名与数据库中的列名相对应。
Case "卡号"
str1(k) = "cardno"
Case "姓名"
str1(k) = "studentName"
Case "上机日期"
str1(k) = "ondate"
Case "上机时间"
str1(k) = "ontime"
Case "下机日期"
str1(k) = "offdate"
Case "下机时间"
str1(k) = "offtime"
Case "消费金额"
str1(k) = "consume"
Case "余额"
str1(k) = "cash"
End Select
Next k
If cmbnexus1.Text = "与" Then '与 或 关系对应SQL语句中的and or。
str2(1) = "and "
End If
If cmbnexus1.Text = "或" Then
str2(1) = "or "
End If
If cmbnexus2.Text = "与" Then
str2(2) = "and "
End If
If cmbnexus2.Text = "或" Then
str2(2) = "or "
End If
txtSQL = "select * from Worklog_Info where " 'SQL语句的前边。
Dim banSQL(3) As String
banSQL(0) = "(" & str1(0) & cmbSymbol1(0).Text & " '" & txtContent1(0).Text & "')" '查询第一条对应SQL语句的一部分,
banSQL(1) = "(" & str1(1) & cmbSymbol1(1).Text & " '" & txtContent1(1).Text & "')" '查询第二条对应SQL语句的一部分。
banSQL(2) = "" & str1(2) & cmbSymbol1(2).Text & " '" & txtContent1(2).Text & "'" '查询第三条对应SQL语句的一部分。
If full(0) = True Then
If cmbnexus1.Text <> "" Then
If empt(1) = True Then
MsgBox "请选择字段名!", vbOKCancel
cmbColName1(1).SetFocus
Exit Sub
Else
If cmbnexus2.Text <> "" Then
If empt(2) = True Then
MsgBox "请选择字段名!", vbOKOnly
Exit Sub
Else
txtSQL = txtSQL & "(" & banSQL(0) & str2(1) & banSQL(1) & ")" & str2(2) & banSQL(2)
End If '查询第三条的SQL语句
Else
txtSQL = txtSQL & banSQL(0) & str2(1) & banSQL(1) '查询第二条SQL语句
End If
End If
Else
txtSQL = txtSQL & banSQL(0) '查询第一条SQL语句
End If
End If
txtSQL = txtSQL & "order by serial"
Set mrc = ExecuteSQL(txtSQL, MsgText)
With MSHFlexGrid1
.CellAlignment = 4
.Rows = 1
.Cols = 9
.TextMatrix(0, 0) = "卡号"
.TextMatrix(0, 1) = "姓名"
.TextMatrix(0, 2) = "上机日期"
.TextMatrix(0, 3) = "上机时间"
.TextMatrix(0, 4) = "下机日期"
.TextMatrix(0, 5) = "下机时间"
.TextMatrix(0, 6) = "消费金额"
.TextMatrix(0, 7) = "余额"
.TextMatrix(0, 8) = "备注"
End With
If mrc.RecordCount = 0 Then
MsgBox "该条件的数据不存在!", vbOKOnly
Exit Sub
End If
Dim a
Dim B
With MSHFlexGrid1
Do While Not mrc.EOF
If IsNull(mrc.Fields(5)) Then
a = ""
Else
a = mrc.Fields(5)
End If
If IsNull(mrc.Fields(6)) Then
B = ""
Else
B = mrc.Fields(6)
End If
.CellAlignment = 4 '查询相对应的数据显示到表中。
.Cols = 9
.Rows = .Rows + 1
.TextMatrix(.Rows - 1, 0) = mrc.Fields(0)
.TextMatrix(.Rows - 1, 1) = mrc.Fields(1)
.TextMatrix(.Rows - 1, 2) = mrc.Fields(2)
.TextMatrix(.Rows - 1, 3) = mrc.Fields(3)
.TextMatrix(.Rows - 1, 4) = mrc.Fields(4)
.TextMatrix(.Rows - 1, 5) = a
.TextMatrix(.Rows - 1, 6) = B
.TextMatrix(.Rows - 1, 7) = mrc.Fields(7)
.TextMatrix(.Rows - 1, 8) = mrc.Fields(8)
mrc.MoveNext
Loop
End With
mrc.Close