我用VB6+ACCESS
    我有一个DataGrid,数据由查询所得,查询条件中有一个日期区间,如2004-03-01至2004-04-30,其中起始日期和终止日期在Text1和Text2中输入。    困惑已久,希望高手给完整代码解决。

解决方案 »

  1.   

    dim strSQL as stringstrSQL="select ... where 日期字段 between #" & text1 & "# and #" & text2 & "#"
      

  2.   

    '数据库为ACCESS:
    strsql=" select * from tablename where 日期字段 between # " & format(text1.text,"YYYY-MM-DD") & # " and " # & format(text2.text,"YYYY-MM-DD") # & "
    '数据库为SQL:
    strsql=" select * from tablename where 日期字段 between ' " & format(text1.text,"YYYY-MM-DD") &"' and '" & format(text2.text,"YYYY-MM-DD") &"'"
      

  3.   

    注意textbox 里的日期格式,建议先用isdate()判断输入是否为日期格式.
    语句为:
    if isdate(text1)=false or isdate(text2)=false then
    msgbox"日期格式有误"
    exit sub
    end if
    sqlstr="select * from table1 where datefield >=#"+text1+"# and datefield<=#"+text2+"#"
      

  4.   

    strsql=" select * from tablename where 日期字段 between # " & format(text1.text,"YYYY-MM-DD") & # " and " # & format(text2.text,"YYYY-MM-DD") # & "
      

  5.   

    我把大家的连起来哈哈哈哈哈  !!!!!!!!!!!!!!!!!!!
    '数据库为ACCESS:
    strsql=" select * from tablename where 日期字段 between # " & format(text1.text,"YYYY-MM-DD") & # " and " # & format(text2.text,"YYYY-MM-DD") # & "
    '数据库为SQL:
    strsql=" select * from tablename where 日期字段 between ' " & format(text1.text,"YYYY-MM-DD") &"' and '" & format(text2.text,"YYYY-MM-DD") &"'"strsql=" select * from tablename where 日期字段 between # " & format(text1.text,"YYYY-MM-DD") & # " and " # & format(text2.text,"YYYY-MM-DD") # & "
      
    注意textbox 里的日期格式,建议先用isdate()判断输入是否为日期格式.
    语句为:
    if isdate(text1)=false or isdate(text2)=false then
    msgbox"日期格式有误"
    exit sub
    end if
    sqlstr="select * from table1 where datefield >=#"+text1+"# and datefield<=#"+text2+"#"
      

  6.   

    用数据环境和SOL语句作一筛选就可了