Private Sub Command1_Click()
Adodc1.RecordSource = "select STCD,sum(isnull(DRP,0)) as DRP From ST_PPTN_R where TM between  cast ('" & DTPicker2.Value & "" & Text3.Text & ":00:00' as datetime )   and   cast ( '" & DTPicker3.Value & "" & Text4.Text & ":00:00' as datetime)  Group By STCD"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1.Recordset
DataGrid1.Refresh
说明:vb6.0中用DTPicker控件来显示日期,用Text控件显示的数值表示整时,分秒默认为:00:00。
select 语句是要查询DTPicker控件和Text控件显示的时间之间的数据。
sql数据库表中字段TM的数据类型是datatime,存储现实是(2011-5-19 18:00:00).
运行上面程序,出现错误是“从char数据类型到datetime数据类型的转换导致datetime值越界”
上面错误怎么改正?或还有其它代码能现实其功能?

解决方案 »

  1.   

    '两个引号之间加个空格吧
    Adodc1.RecordSource = "select STCD,sum(isnull(DRP,0)) as DRP From ST_PPTN_R where TM between  '" & DTPicker2.Value & " " & Text3.Text & ":00:00' and '" & DTPicker3.Value & " " & Text4.Text & ":00:00'  Group By STCD"
      

  2.   

    CTOD( ) Function
    See AlsoConverts a character expression to a date expression.SyntaxCTOD(cExpression)ReturnsDateArgumentscExpressionSpecifies a character expression for which CTOD( ) returns a Date-type value.
      

  3.   

    format(dtpicker2.Value,"yyyy-mm-dd")
    这样试试
      

  4.   

    where tm between '"& format(dtpicker2.value,"YYYY-MM-DD") & " " & text3.text & ":00:00' and '"& format(dtpicker3.value,"YYYY-MM-DD") & " " & text4.text &":00:00'
      

  5.   

    format(dtpicker2.Value,"yyyy-mm-dd") & “ ” & text3.text & ":00:00"
      

  6.   

    运行后弹出betweem附件语法错误。
      

  7.   

    between后面是日期,日期用单引号肯定不行,不转换更不行,数据库里的数据不是日期型也不行
      

  8.   

    TM>= ... and TM<=...
      

  9.   

    帮忙改正下。数据库里的数据类型是datetime
      

  10.   

    http://download.csdn.net/source/1644211