SQL语句条件中日期字段如何写-常樂
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-查询/SQL语句


SQL语句条件中日期字段如何写

发表时间:2018/4/28 11:50:31 评论(0) 浏览(11903)  评论 | 加入收藏 | 复制
   
摘 要:SQL语句条件中日期字段如何写
正 文:

构成SQL语句时日期直接使用是不行的,即使变量是日期型,但构成SQL语句时还是会被当成字符串处理的,要想把字符串转换称日期型数据,需要用“#”括起来,例如下面这个统计过程:


Private Sub btnOK_Click()
    Dim StartDate As Date, EndDate As Date
    Dim strSQL, strItem As String
    
    DoCmd.SetWarnings False
    DoCmd.RunSQL "Delete from tblTransSum"
    
    '统计日期
    StartDate = Nz(Me.txtStartDate)
    EndDate = Nz(Me.txtEndDate)
    If StartDate = 0 or EndDate = 0 Then
        MsgBox "请先选择日期,然后点击统计按钮!", vbCritical, "提示"
        Exit Sub
    End If
    '统计单位
    If Me.frmItem = 1 Then
        strItem = "FAbbr"
    Else
        strItem = "CAbbr"
    End If
    '统计
    strSQL = "Insert into tblTransSum (Item,ExtraFee,DeductFee,SettleFee,CExtraFee,CDeductFee,CSettleFee) Select " & strItem
    strSQL = strSQL & ",sum(ExtraFee),sum(DeductFee),sum(SettleFee),sum(CExtraFee),sum(CDeductFee),sum(CSettleFee) from tblTransport"
    strSQL = strSQL & " Where AuditState and AuditDate Between #" & StartDate & "# and #" & EndDate & "# Group by " & strItem
    DoCmd.RunSQL strSQL
    '插入一条合计
    strSQL = "Insert into tblTransSum (Item,ExtraFee,DeductFee,SettleFee,CExtraFee,CDeductFee,CSettleFee) Select '合计'"
    strSQL = strSQL & ",sum(ExtraFee),sum(DeductFee),sum(SettleFee),sum(CExtraFee),sum(CDeductFee),sum(CSettleFee) from tblTransSum"
    '刷新显示
    RequeryDataObject Me.sfrList
End Sub


Access软件网交流QQ群(群号:198465573)
 
 相关文章
[access查询]日期字段为空如何设?  【Suve  2007/12/16】
Acces开发平台--数值型变量在修改与删除功能中的使用方法\数字...  【褚玉春  2009/4/23】
【Access扫盲知识】声明模块变量的用法,变量符号用法,文本型变...  【麥田  2013/2/26】
快速开发平台--自动编号关联日期字段/自动编号根据日期字段动态编号  【宏鹏  2016/11/3】
快速开发平台--日期字段进行筛选不会自动出现区间选择问题  【杨雪  2017/2/14】
常见问答
技术分类
相关资源
文章搜索
关于作者

常樂

文章分类

文章存档

友情链接