机房收费系统之—怎样查询两个日期之间的数据

日历控件刚刚学会怎么弄,但是要查询这两个语句之间的数据的语法我可是较劲脑汁啊,在网上查,自己试,可是每次不是语法错误就是连接不上数据库,我感觉这个方法就在我的眼前,我就是弄不出来,煞费苦心啊。下面是查询两个日历控件之间的代码,大家可以看看。

<pre name="code" class="vb"><span style="font-size:18px;">Private Sub Command1_Click()
    date1 = DTPicker1.Value
    date2 = DTPicker2.Value
    '比较两个日期的大小
    If DateDiff("n",CDate(date1),CDate(date2)) < 0 Then
        MsgBox "起始日期与结束日期有冲突,请重新选择日期","提示"
        Exit Sub
    End If
    
     Dim i,iCols As Integer '让所有列的文字都居中显示
     iCols = MSFlexGrid1.Cols
     
     For i = 0 To iCols - 1
        MSFlexGrid1.ColAlignment(i) = flexAlignCenterCenter
     Next
    
    txtSQL = "select * from Recharge_Info where date between '" & date1 & "'" & " and '" & date2 & "'"
    Set mrc = ExecuteSQL(txtSQL,Msgtext)
    If mrc.EOF = True Then
        MsgBox "没有信息"
        Exit Sub
    End If
    With MSFlexGrid1
    
        Do While mrc.EOF = False
            .Rows = .Rows + 1
            .TextMatrix(.Rows - 1,0) = mrc!cardno
            .TextMatrix(.Rows - 1,1) = mrc!addmoney
            .TextMatrix(.Rows - 1,2) = mrc!Date
            .TextMatrix(.Rows - 1,3) = mrc!Time
            .TextMatrix(.Rows - 1,4) = mrc!userID
            .TextMatrix(.Rows - 1,5) = mrc!Status
            mrc.MoveNext
        Loop
    End With
    mrc.Close
End Sub
</span>


 
 写了一大堆,其实最有用的就是txtSQL语句 
这句话—— 
<pre name="code" class="vb">txtSQL = "select * from Recharge_Info where date between'" & Format$(date1,"yyyy - mm - dd") & "'" & " and'" & Format$(date2,"yyyy-mm-dd") & "'"
txtSQL = "select * from Recharge_Info where date >='" & date1 & "' and date <='" & date2 & "'"
 
上面的两句话写那句也行,看我们的不同需要了。 


此方法不仅可以比较日期,以此类推,基本上带数字的都能查询吧!其实在数据这,我们把select 语句玩转了可以说把数据库也快玩转了,这个语法,与之前用等于号的语法 比较起来,也有相似之处,有了相似,我们就好记了。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...