我设置了4个DTPicker,分别为DTPicker1,DTPicker2,DTPicker3,DTPicker4.
StrSQL = "select * from 员工档案 where "
str1= " 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" & ""
str2= " 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""
怎样才能同时满足这两个条件实施查询??
我是这样写的StrSQL = StrSQL & str2 & str1,系统提示语法错误,
要是这样的话 
StrSQL =& " 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" & "" and &" 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""也是错误的
请帮帮忙.

解决方案 »

  1.   

    StrSQL ="(入司时间 between " & DTPicker1.Value & " and " & DTPicker2.Value & ") and & " (出生年月 between " & DTPicker3.Value & " and " & DTPicker4.Value & ")"
      

  2.   

    StrSQL ="(cast(入司时间 as date) between " & DTPicker1.Value & " and " & DTPicker2.Value & ") and & " (cast(出生年月 as date) between " & DTPicker3.Value & " and " & DTPicker4.Value & ")"
      

  3.   

    原来代码应该没错,去掉多余的"&"试试:StrSQL = "select * from 员工档案 where "
    str1=" 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "# " str2=" 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" 
    StrSQL = StrSQL & str2 & str1如果不行可能和DTpicker1 Value的类型有关,可以定义4个变量代替
    dim d1 as date 
    dim d2 as date 
    dim d3 as date 
    dim d4 as date d1=DTPicker1.value
    ...
    ...
    str1=" 入司时间 between #" & d1 & "# and #" & d2 & "# " 
    str2=" 出生年月 between #" & d3 & "# and #" & d4 & "#" 
    ...
      

  4.   

    Private Sub Command1_Click()
    Dim StrSQL As String
    StrSQL = "select * from 员工档案 where "
    if Check1 = 1 AND Check2=1 Then
    StrSQL = StrSQL & " 入司时间 between #" & DTPicker1.Value & "# and #" &  DTPicker2.Value & "#" & "" & " 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""
    end if 
     If rs3.State = adStateClosed Then
                    rs3.Open StrSQL, DBCON, adOpenKeyset, adLockOptimistic, adCmdText
                End If           
            Set VSFlexGrid2.DataSource = rs3.DataSource
            Label14.Caption = rs3.RecordCount
            VSFlexGrid2.Refresh
            rs3.Close
    这是我的代码,错误原因是"where子句语法错误",以上的我也试过了,还是不行,不知道怎么改了,脑袋大了
      

  5.   

    你的数据库用的上access还是SQL Server,如果是SQl Server,那就不用#号,还有,你把所有的#" & DTPicker.Value & "# 改成 #'" & DTPicker.Value & "'# 
    或者'" & DTPicker.Value & "'试试。
      

  6.   

    我的是access,刚才的方法我试了,还是不行,语法错误.str1= " 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" & ""
    str2= " 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""
    这两个时间条件难道就不能同时满足吗?大家帮帮忙啊
      

  7.   

    我的是access,刚才的方法我试了,还是不行,语法错误.str1= " 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" & ""
    str2= " 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""
    这两个时间条件难道就不能同时满足吗?大家帮帮忙啊
      

  8.   

    DTPicker1.Value取出来是完整的yyyymmdd hhmmss的时间值,你access数据库里字段是什么格式的?建议把DTPicker1.Value先用format转换一下先。
      
    *****************************************************************************
    菜鸟一只
      

  9.   

    如果str1和str2都没错的话,那么用“and” 连接起来应该是没错的
    建议在数据库中输入一个同时符合两个条件的记录试试str1= " 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" & ""
    str2= " 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""StrSQL = "select * from 员工档案 where " + str1 +  " and " + str2
      

  10.   

    问题解决,非常感谢stlyl(stlyl(维生素B)) ,不知道怎样才能给你点数,请指教,谢谢!!!
      

  11.   

    会给点数了,再次谢谢stlyl(stlyl(维生素B)) ,也谢谢大家这些天对我的帮助,好人真多
      

  12.   

    str1= " 入司时间 between #" & DTPicker1.Value & "# and #" & DTPicker2.Value & "#" & ""
    str2= " 出生年月 between #" & DTPicker3.Value & "# and #" & DTPicker4.Value & "#" & ""
    str3="工号= '" & Trim(Text1(0).Text) & "'"
    str4="姓名= '" & Trim(Text1(1).Text) & "'"
    请教一下stlyl(stlyl(维生素B)) ,这个怎么连??我按你的方法,时间的问题是解决了,不过这四个又有语法错误了,请指教,谢谢