在做机房的时候遇到查询时间段的问题,起初的时候就感觉无从下手,后来想了起来,耿建玲老师的视频里讲过两个范围之间的查询,于是就翻翻笔记,问题就解决了,下面把代码贴出来。
Private Sub Command1_Click()
Dim txtSQL As String
Dim MsgText As String
Dim mrc As ADODB.Recordset
If DTPicker1.Value > DTPicker2.Value Then‘当前选中的时间
MsgBox "起始日期不能比终止日期早!", vbOKOnly + vbExclamation, "提示!"
DTPicker1.SetFocus
With MSHFlexGrid1’清空控件内容
.Col = 2
.Rows = 1
.Text = ""
End With
Exit Sub
End If
With MSHFlexGrid1
.Rows = 2
.CellAlignment = 4
.TextMatrix(1, 0) = "卡号"
.TextMatrix(1, 1) = "充值金额"
.TextMatrix(1, 2) = "充值日期"
.TextMatrix(1, 3) = "时间"
.TextMatrix(1, 4) = "充值教师"
.TextMatrix(1, 5) = "结账状态"
txtSQL = "select * from ReCharge_Info where date between '" & DTPicker1.Value & " ' and '" & DTPicker2.Value & "'" ’查询选取时间段的数据
Set mrc = ExecuteSQL(txtSQL, MsgText)
If mrc.EOF = True Then ‘该段没有符合的条件
MsgBox "对不起,该时期没有充值记录!", vbOKOnly + vbExclamation, "提示"
Exit Sub
End If
Do While mrc.EOF = False
.Rows = .Rows + 1
.CellAlignment = 4
.TextMatrix(.Rows - 1, 0) = mrc.Fields(2)
.TextMatrix(.Rows - 1, 1) = mrc.Fields(3)
.TextMatrix(.Rows - 1, 2) = mrc.Fields(4)
.TextMatrix(.Rows - 1, 3) = mrc.Fields(5)
.TextMatrix(.Rows - 1, 4) = mrc.Fields(6)
.TextMatrix(.Rows - 1, 5) = mrc.Fields(7)
mrc.MoveNext
Loop
mrc.Close
End With
End Sub
这整个过程需要格外注意三点:1是
DTPicker1.Value表示当前选中的日期。2是MSHFlexGrid控件的清除,这里参考了李红同学的博客。3就是最重要的Select 字句的查询:Select * from 【表】where 【查询字段】between ‘’and ‘’