用的是ADO查询
txtsql = "select * from table1 where Chart_fre=" & CInt(TxtFre.Text) & " and Chart_operate='" & Trim(Cmbopa.Text) & "' and Chart_Stime >= #" & DTpsta.Value & "# and Chart_Etime <= #" & DTPend.Value & "#" 'Set mrc_cha = Execute_SQL(txtsql, MsgText, cnn)Public Function Execute_SQL(ByVal SQL _
   As String, MsgString As String, _
   conn As ADODB.Connection) _
   As ADODB.Recordset   Dim Rst As ADODB.Recordset
   Dim sTokens() As String
   
   On Error GoTo Execute_SQL_Error
   
   sTokens = Split(SQL)
   
   If InStr("INSERT,DELETE,UPDATE", _
      UCase$(sTokens(0))) Then
      conn.Execute SQL
      MsgString = sTokens(0) & _
         " query successful"
   Else
      Set Rst = New ADODB.Recordset
      Rst.Open SQL, conn, _
         adOpenKeyset, _
         adLockOptimistic
      'rst.MoveLast     'get RecordCount
      Set Execute_SQL = Rst
      MsgString = "查询到" & Rst.RecordCount & _
         " 条记录 "
   End If
Execute_SQL_Exit:
   Set Rst = Nothing
   Exit Function
   
Execute_SQL_Error:
   MsgString = "查询错误: " & _
      Err.Description
   Resume Execute_SQL_Exit
End Function
关键是时间段的查询,执行后,有的记录可以查询到,有的记录查询不到,请教高手这是什么原因?

解决方案 »

  1.   

    用between and 语句试试txtsql = "select * from table1 where Chart_fre=" & CInt(TxtFre.Text) & " and Chart_operate='" & Trim(Cmbopa.Text) & "' and Chart_Stime between #" & DTpsta.Value & "# and  #" & DTPend.Value & "#" '
      

  2.   

    http://www.kekecn.com/blog/article.asp?id=150
      

  3.   

    txtsql = "select * from table1 where Chart_fre=?and Chart_operate=?and Chart_Stime between ?and  ?"
    然后把四个?的值传进去。
    用ADO的Command对象来做。这样的好处是,SQL语句直观,易懂。
      

  4.   

    这两个时间不是同一个字段啊,怎么用between and 语句啊?
      

  5.   

    估计是数据格式不一样造成的(比如:9:9:9 与 09:9:09)用Format函数将数据格式化,然后再做比较:txtsql = "select * from table1 where Chart_fre=" & CInt(TxtFre.Text) & " and Chart_operate='" & Trim(Cmbopa.Text) & "' and Format(Chart_Stime,'hh:mm:ss') >= #" & Format(DTpsta.Value, "hh:mm:ss") & "# and Format(Chart_Etime,'hh:mm:ss') <= #" & Format(DTPend.Value, "hh:mm:ss") & "#" '
      

  6.   

    不行啊,Format(Chart_Stime,'hh:mm:ss')显示语法有问题
      

  7.   

    Format(Chart_Stime,"hh:nn:ss")
      时间格式化
      

  8.   

    不行啊,Format(Chart_Stime,'hh:mm:ss')显示语法有问题
    ---------------------------------------------------------
    用Format没问题哦,可以看看你的sql语句吗
      

  9.   

    Format(Chart_Stime,"hh:nn:ss")
    要用双引号
      

  10.   

    在vb中是用双引号,但是,如果是在sql语句中,则用单引号,具体的请看上面的代码
      

  11.   

    对,Format(Chart_Stime,'hh:mm:ss')没有问题,我输成双引号了,但是修改后,问题依旧存在,有的记录能查询到,有的查询不到
      

  12.   

    上面 #" & DTpsta.Value & "#  
     此DTpsta.value已是'日期型'的数据了,两边#号就不需要。
     如是上面是textbox控件传过来的值就要在两边加#号。
     
      

  13.   

    我用的数据库是ACESS数据库,把#号去掉就会出现错误.SQL SERVER数据库不用#
      

  14.   

    首先你要先排除你的表和数据是不是有问题。SQL语句中用过format函数后,可以不用#,改用单引号,试试看
    txtsql = "select * from table1 "& _
      " where Chart_fre=" & CInt(TxtFre.Text) & _
      " and Chart_operate='" & Trim(Cmbopa.Text) & "'"& _
      " and Format(Chart_Stime,'hh:mm:ss') >='"& Format(DTpsta.Value,'hh:mm:ss')&"'"& _
      " and Format(Chart_Etime,'hh:mm:ss') <='"& Format(DTPend.Value,'hh:mm:ss') &"'" 你可以用debug.print看看这样的SQL语句是不是符合你的要求
      

  15.   

    用debug.print txtsql  把查询语句打印到立即窗口,看看具体是什么样子,贴出来看看,最好连txtsql=.....语句也一块贴出来,让大家分析