机房收费系统之收取金额查询(TPicker控件时间段取值、SQL语句中单引号与双引号区别)

        收取金额查询窗体较组合查询而言就是小菜一碟,但即便是内容较少也有其精华之处,现在分享一下我的学习过程吧^_^

一、收取金额查询窗体的流程图:

二、问题集

这是什么错误呢,为什么会出现这个类型的错误?

产生此问题的代码部分是什么样子的呢?

以下为问题代码:

    txtSQL = "select * from Recharge_Info where date > " & begindate.Value & "   and  date< " & enddate.Value & ""
    Set mrc = ExecuteSQL(txtSQL, MsgText)

 你能看出问题出在哪里么?

正确代码如下:

    txtSQL = "select * from Recharge_Info where date > '" & begindate.Value & "'   and  date< '" & enddate.Value & "'"
    Set mrc = ExecuteSQL(txtSQL, MsgText)

看出他们的区别了么?

提示一下问题出在符号上面哦^_^

是的,问题就出在了单引号上面,那么SQL中单引号与双引号的区别是什么呢?

①双引号里面的字段会经过编译器解释然后再当作代码输出,而单引号里面的不需要解释,直接输出。

②SQL里字符型只能用单引号,双引号是引用的链接数据库的程序里的,如果要用双引号,在SQL里要加个单引号。

三、代码集

Private Sub cmdquery_Click()
    Dim mrc As ADODB.Recordset
    Dim txtSQL As String
    Dim MsgText As String
    
    '查询选定范围里的数据
    txtSQL = "select * from Recharge_Info where date > '" & begindate.Value & "'   and  date< '" & enddate.Value & "'"
    Set mrc = ExecuteSQL(txtSQL, MsgText)
    
    If mrc.EOF Then                       '无数据时提示
        MsgBox "该时间段无数据", vbOKOnly, "温馨提示:"
        Exit Sub
    End If
    
    With myflexgrid                       '有数据逐个加载
        .Rows = 1
        .CellAlignment = 4
        .TextMatrix(0, 0) = "卡号"
        .TextMatrix(0, 1) = "充值金额"
        .TextMatrix(0, 2) = "充值日期"
        .TextMatrix(0, 3) = "充值时间"
        .TextMatrix(0, 4) = "充值操作员"
        .TextMatrix(0, 5) = "结账状态"
        Do While Not mrc.EOF                        '循环加载
            .Rows = .Rows + 1
            .CellAlignment = 4
            .TextMatrix(.Rows - 1, 0) = Trim(mrc.Fields(2))
            .TextMatrix(.Rows - 1, 1) = Trim(mrc.Fields(3))
            .TextMatrix(.Rows - 1, 2) = Trim(mrc.Fields(4))
            .TextMatrix(.Rows - 1, 3) = Trim(mrc.Fields(5))
            .TextMatrix(.Rows - 1, 4) = Trim(mrc.Fields(6))
            .TextMatrix(.Rows - 1, 5) = Trim(mrc.Fields(7))
            mrc.MoveNext
        Loop
    End With

End Sub

      机房的窗体已经敲完一半,明显感觉现在对代码的亲切度比以前要深入了很多,敲代码的速度也有所见长,这也是日积月累的过程吧,虽然现在每天的计算机时间很少,只要不断就是好的,下一站走起^_^

猜你喜欢

转载自blog.csdn.net/Elsa15/article/details/82958311