当前位置:编程学习 > VB >>

select 查询时间段的问题

用两个DTPicker控件选择时间,查询处在该时间段内的数据库中的数据,并在datagrid中显示,用ado控件
不知道是不是语句的问题,不能运行
贴代码
Private Sub command1_Click()
    '查询条件校验
    If DTPicker1.Value = 0 Then
        MsgBox "没有定义查询条件,请定义!"
        Exit Sub
    End If
    If DTPicker2.Value = 0 Then
        MsgBox "没有定义查询条件,请定义!"
        Exit Sub
    End If
    
    '查询条件确认
    Dim sSql As String: sSql = "1=1"
    If chkDate.Value = 1 Then
        sSql = sSql & " AND 涂装计划.时间 between #" & Format(DTPicker1, "yy-mm-dd") & "# AND #" & Format(DTPicker2, "yy-mm-dd") & "#"
    End If
    
    '开始查询
    Adodc1.RecordSource = "select * from 涂装计划 where 时间between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
Set DataGrid1.DataSource = Adodc1 '与adodc绑定
DataGrid1.Refresh

End Sub

Private Sub command1_Click()
  '查询条件校验
  If DTPicker1.Value ="" Then
      MsgBox "没有定义查询条件,请定义!"
      Exit Sub
  End If
  If DTPicker2.Value ="" Then
      MsgBox "没有定义查询条件,请定义!"
      Exit Sub
  End If
    
  '查询条件确认
  Dim sSql As String
  sSql = "1=1"
  If chkDate.Value = 1 Then
      sSql = sSql & " AND 涂装计划.时间 between #" & Format(DTPicker1.value, "yy-mm-dd") & "# AND #" & Format(DTPicker2.value, "yy-mm-dd") & "#"
  End If
    
  '开始查询
   Adodc1.RecordSource = "select * from 涂装计划 where"&sSql
  Set DataGrid1.DataSource = Adodc1 '与adodc绑定
   DataGrid1.Refresh

End Sub

数据库是ACCESS:
Adodc1.RecordSource = "select * from 涂装计划 where 时间 between #" & DTPicker1.Value & " 00:00:00# and #" & DTPicker2.Value & " 23:59:59#"
数据库是SQL:
Adodc1.RecordSource = "select * from 涂装计划 where 时间 between '" & DTPicker1.Value & " 00:00:00' and '" & DTPicker2.Value & " 23:59:59'" 一看就知道这是从贴吧过来的,被某个大忽悠坑害的
先不管你sql语句是否正确,以下才是正确邦定方法。


'开始查询

 Adodc1.RecordSource = "select * from 涂装计划 where 时间between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
 Adodc1.Refresh
 Set DataGrid1.DataSource = Adodc1 '与adodc绑定
ProductRecord.ProduceDate & ProductRecord.ProduceTime between '" & Format(DTPicker3.Value, "yyyy-mm-dd") & "" & Format(DTPicker4.Value, "hh:mm:ss") & "' and '" & Format(DTPicker5.Value, "yyyy-mm-dd") & "" & Format(DTPicker6.Value, "hh:mm:ss") & "' order by ID asc" 给你参照一下 我一般用datediff()>0 and datediff()>0 from子句语法错误。
引用楼主 zenmedouyaozhuce 的回复:
用两个DTPicker控件选择时间,查询处在该时间段内的数据库中的数据,并在datagrid中显示,用ado控件
不知道是不是语句的问题,不能运行
贴代码
Private Sub command1_Click()
  '查询条件校验
  If DTPicker1.Value = 0 Then
  MsgBox "没有定义查询条件,请定义!"
  Exit Sub
  End If
……
不要有几分几秒的,只要年月日就可以,有关时间真是头大
引用 6 楼 woodepany 的回复:
给你参照一下
Private Sub command1_Click()
    '查询条件校验
    If DTPicker1.Value = 0 Then
        MsgBox "没有定义查询条件,请定义!"
        Exit Sub
    End If
    
    If DTPicker2.Value = 0 Then
        MsgBox "没有定义查询条件,请定义!"
        Exit Sub
    End If
    
    '查询条件确认
    Dim sSql As String: sSql = "1=1"
    
    If chkDate.Value = 1 Then
        sSql = sSql & " AND 涂装计划.时间 between #" & Format(DTPicker1, "yy-mm-dd") & "# AND #" & Format(DTPicker2, "yy-mm-dd") & "#"
    End If
    
    '开始查询
    Adodc1.RecordSource = "select * from 涂装计划 where " & sSql
    
    Set DataGrid1.DataSource = Adodc1 '与adodc绑定
    Adodc1.Refresh


End Sub
Private Sub command1_Click()
    '查询条件校验
    If DTPicker1.Value = 0 Then
        MsgBox "没有定义查询条件,请定义!"
        Exit Sub
    End If
    
    If DTPicker2.Value = 0 Then
        MsgBox "没有定义查询条件,请定义!"
        Exit Sub
    End If
    
    '查询条件确认
    Dim sSql As String: sSql = "1=1"
    
    If chkDate.Value = 1 Then
        sSql = sSql & " AND 涂装计划.时间 between #" & Format(DTPicker1, "yy-mm-dd") & "# AND #" & Format(DTPicker2, "yy-mm-dd") & "#"
    End If
    
    '开始查询
    Adodc1.RecordSource = "select * from 涂装计划 where " & sSql
    Set DataGrid1.DataSource = Adodc1 '与adodc绑定
    Adodc1.Refresh

End Sub
应该这样写 Adodc1.RecordSource = "select * from 涂装计划 where 计划生产时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" 应该这样写 Adodc1.RecordSource = "select * from 涂装计划 where 计划生产时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"
引用 12 楼 zenmedouyaozhuce 的回复:
应该这样写 Adodc1.RecordSource = "select * from 涂装计划 where 计划生产时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#"


同意
补充:VB ,  数据库(包含打印,安装,报表)
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,