我写了一个查询语句,关于客人在一段时间内入住客房的记录。
'text1用于输入客人姓名
txtsql="select * from tablename where 客人姓名 like '%" & text1.text & "%'"
'text2,text3用于输入查询的时间段
txtsql=txtsql & "and (入住时间>='#" & text2.text & "#' and 入住时间<='#" & text3.text & "#')"
请高手帮忙看看,哪写错了,编译时提示类型错误。谢谢!

解决方案 »

  1.   

    时间查询那里错误!你可以#1999年6月20日#,但是你不能#text1.text#.你可以dim a as date
    a=text1.text
    然后直接使用a 来查询就可以了
      

  2.   

    首先你用的是ACCESS数据库还是SQLSERVER?
    如果使用SQLSERVER,日期时间类型是不是“#”号来标是明的,而是用“'”(和字符串类型一样)。然后对于时间字段的比较最好使用DATEDIFF函数或DATEADD函数,关于使用方法请看SQLSERVER帮助手册,里面说得很详细。
      

  3.   

    1、and前要加一个空格;
    2、你的text2.text和text3.text要是日期型;
    3、你的数据库用access的话就用#,是SQL Server的话要用'。改为:
    'text1用于输入客人姓名
    txtsql="select * from tablename where 客人姓名 like '%" & trim(text1.text) & "%'"
    'text2,text3用于输入查询的时间段
    txtsql=txtsql & "and (入住时间>='#" & cdate(format(text2.text,"yyyy-mm-dd")) & "#' and 入住时间<='#" & cdate(format(text3.text,"yyyy-mm-dd")) & "#')"^_^
      

  4.   

    txtsql=txtsql & "and (入住时间>='" & text2.text & "' and 入住时间<='" & text3.text & "')"
    sql server的数据库就上面那样写就好了,如果是access的话就用下面的:
    txtsql=txtsql & "and (入住时间>=#" & text2.text & "# and 入住时间<=#" & text3.text & "#)"
      

  5.   

    我有时会这样用(sqlserver)
    txtsql=txtsql & "入住时间 between '" & text2.text  & " and '" & text3.text & "'"
    效果一样好:)