那张帖子30分,还是没有解决问题。100分吧,希望能得到解决!!!原帖子地址:
http://community.csdn.net/Expert/topic/4987/4987851.xml?temp=.9855158
procedure TSarch.FormCreate(Sender: TObject);
begin
 datetimepicker1.Date:=now();
 datetimepicker2.Date:=now();
 edit1.Clear;
SetWindowLong(Edit1.Handle, GWL_STYLE,
GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);  RZcombobox1.Clear;
  with data1.Sarch do
  begin
    close;
    sql.Clear;
    sql.Add('select distinct model from Common ');
    open;
  end;
  if data1.Sarch.RecordCount>0 then
  begin
    while not data1.Sarch.Eof do
    begin
      rzcombobox1.Items.Add(data1.Sarch.fieldbyname('model').AsString);
      data1.Sarch.Next;
    end;
  end;//   列出生产线   / /  with data1.Sarch do
  begin
    close;
    sql.Clear;
    sql.Add('select distinct line from Common ');
    open;
  end;
  if data1.Sarch.RecordCount>0 then
  begin
    while not data1.Sarch.Eof do
    begin
      rzcombobox3.Items.Add(data1.Sarch.fieldbyname('line').AsString);
      data1.Sarch.Next;
    end;
  end;
    with data1.Sarch do
  begin
    close;
    sql.Clear;
    sql.Add('select distinct class from Common ');
    open;
  end;
  if data1.Sarch.RecordCount>0 then
  begin
    while not data1.Sarch.Eof do
    begin
      rzcombobox4.Items.Add(data1.Sarch.fieldbyname('class').AsString);
      data1.Sarch.Next;
    end;
  end; //  列出班次  with data1.Sarch do
  begin
    close;
    sql.Clear;
    sql.Add('select distinct shift from Common ');
    open;
  end;
  if data1.Sarch.RecordCount>0 then
  begin
    while not data1.Sarch.Eof do
    begin
      rzcombobox2.Items.Add(data1.Sarch.fieldbyname('shift').AsString);
      data1.Sarch.Next;
    end;
  end;
  with data1.Sarch do
  begin
    close;
    sql.Clear;
    sql.Add('select  shift from common');
    open;
  end; end;
procedure TSarch.BitBtn1Click(Sender: TObject);   var
   i:integer;
  low,high:string;
begin
   s:='select * from information  ';
   i:=0;
   if Rzcombobox1.Text<>'' then
   begin      if i=0 then
         s:=s+'  where model='''+Rzcombobox1.Text+''''
      else
         s:=s+' and  model='''+Rzcombobox1.Text+'''';
      i:=i+1;
   end;
   if Edit1.Text<>'' then
   begin
      if i=0 then
         s:=s+'  where sampleID='''+Edit1.Text+''''
      else
         s:=s+' and  sampleID='''+Edit1.Text+'''';
      i:=i+1;
   end;      if Rzcombobox2.Text<>'' then
   begin      if i=0 then
         s:=s+' where shift='''+Rzcombobox2.Text+''''
      else
         s:=s+' and  shift='''+Rzcombobox2.Text+'''';
      i:=i+1;
   end;
      if Rzcombobox3.Text<>'' then
   begin      if i=0 then
         s:=s+'where line='''+Rzcombobox3.Text+''''
      else
         s:=s+' and  line='''+Rzcombobox3.Text+'''';
      i:=i+1;
   end;
   if Rzcombobox4.Text<>'' then
   begin      if i=0 then
         s:=s+' where grade='''+Rzcombobox4.Text+''''
      else
         s:=s+' and  grade='''+Rzcombobox4.Text+'''';
      i:=i+1;
   end;      if Rzcombobox5.Text<>'' then
   begin      if i=0 then
         s:=s+' where parts='''+Rzcombobox5.Text+''''
      else
         s:=s+' and  parts='''+Rzcombobox5.Text+'''';
      i:=i+1;
   end;   low:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
   high:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
   if i=0 then
      s:=s+'where '
   else
      s:=s+' and ';
   s:=s+'workdate<'''+high+''' and workdate>'''+low+'''';
  ADOQuery1.Connection:=ADOConnection1;
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add(s);
  ADOQuery1.Open;end;
这段代码错在那里?
--------------------------------------------------------   low:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
   high:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
   if i=0 then
      s:=s+'where '
   else
      s:=s+' and ';
   s:=s+'workdate<'''+high+''' and workdate>'''+low+'''';
---------------------同过两个datatimepicker1 和datatimepicker1 查询某一段日期类的记录。程序编译能通过,但是一旦提交“查询”,就报错“标准表达试中类型不匹配”
什么原因?要如何写?还有什么办法可以实现这个功能?

解决方案 »

  1.   

    --------------------------------------------------------   low:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker1.Date);
       high:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
       if i=0 then
          s:=s+'where '
       else
          s:=s+' and ';
       s:=s+'workdate<'''+high+''' and workdate>'''+low+'''';
    ---------------------
    这段代码有问题吗?要如何写?重要能功能实现,用什么办法都可以,谢谢
      

  2.   

    在这儿把s打印出来看看是什么,可以把s复制到查询分析器中测试看看(如果是MSSQL)ADOQuery1.SQL.Add(s);//
      

  3.   

    看了你原贴,是access数据库用参数传递吧,例如:adoquery1.sql.text := 'select * from table1 where col1 = :col1 and col2 = :col2';
    adoquery1.parameters.ParamByName('col1').Value := FormatDateTime('yyyy-mm-dd',DateTimepicker1.Date);
    adoquery1.parameters.ParamByName('col2').Value := FormatDateTime('yyyy-mm-dd',DateTimepicker1.Date);
    adoquery1.open
      

  4.   

    代码太长,没仔细看,如果就是access数据库日期类型参数赋值的问题,就按照楼上说的方法做
      

  5.   

    改为:
    begin
       mStr := 'select * from information  where ';
       s := '';
       
       if Rzcombobox1.Text<>'' then
             s:=s+'model='''+Rzcombobox1.Text+''' and ';   if Edit1.Text<>'' then
             s:=s+'sampleID='''+Edit1.Text+''' and ';   if Rzcombobox2.Text<>'' then
             s:=s+'shift='''+Rzcombobox2.Text+''' and ';   if Rzcombobox3.Text<>'' then
             s:=s+line='''+Rzcombobox3.Text+''' and ';   if Rzcombobox4.Text<>'' then
             s:=s+'grade='''+Rzcombobox4.Text+''' and ';   if Rzcombobox5.Text<>'' then
             s:=s+'parts='''+Rzcombobox5.Text+''' and ';   low:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       high:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       s:=mStr + s + 'workdate<'''+high+''' and workdate>'''+low+'''';
      ADOQuery1.Connection:=ADOConnection1;
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      ADOQuery1.SQL.Add(s);
      ADOQuery1.Open;end;
      

  6.   

    sql server上是可以的,如果是access需要改一下
      

  7.   

    sql server上是可以的,如果是access需要改一下.
    怎么改?
      

  8.   

    procedure TSarch.BitBtn1Click(Sender: TObject);
    var mStr:string;
    var low,high:string;
    begin
       mStr := 'select * from information  where ';
       s := '';
       
       if Rzcombobox1.Text<>'' then
             s:=s+'model='''+Rzcombobox1.Text+''' and ';   if Edit1.Text<>'' then
             s:=s+'sampleID='''+Edit1.Text+''' and ';   if Rzcombobox2.Text<>'' then
             s:=s+'shift='''+Rzcombobox2.Text+''' and ';   if Rzcombobox3.Text<>'' then
             s:=s+'line='''+Rzcombobox3.Text+''' and ';   if Rzcombobox4.Text<>'' then
             s:=s+'grade='''+Rzcombobox4.Text+''' and ';   if Rzcombobox5.Text<>'' then
             s:=s+'parts='''+Rzcombobox5.Text+''' and ';   low:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker1.Date);
       high:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
       s:=mStr + s + 'workdate<'''+high+''' and workdate>'''+low+'''';
      ADOQuery1.Connection:=ADOConnection1;
      ADOQuery1.Close;
      ADOQuery1.SQL.Clear;
      ADOQuery1.SQL.Add(s);
      ADOQuery1.Open;end;
    改成这样,还是不行。  糊涂了~~~~~~~~~~
      

  9.   

    low:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker1.Date);
       high:=formatdatetime('yyyy"-"mm"-"dd',DateTimePicker2.Date);
       s:=mStr + s + 'workdate<'''+high+''' and workdate>'''+low+'''';
    这几句没改啊!!
      

  10.   

    如果是access,改成
       low:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       high:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       s:=mStr + s + 'workdate<#'+high+'# and workdate>#'+low+'#';
    如果是sql server 改成
       low:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       high:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       s:=mStr + s + 'workdate<'''+high+''' and workdate>'''+low+'''';
      

  11.   

    楼上的,你所说两种方法在SQL和ACCESS中都是一回事,都能用吧? 我没详细看帖子,但我觉得设段点一步步的跟踪就能看到问题了,或者把SQL语句弄在查询分析器里一看就什么都知道了.还这样讨论得这么复杂~!
      

  12.   

    erhan(二憨) ( )太谢谢你了。问题解决了,我现在只想问一句,Access为什么和Sql方法会不同? 再一次谢谢!!!   low:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       high:=formatdatetime('yyyy-mm-dd',DateTimePicker2.Date);
       s:=mStr + s + 'workdate<#'+high+'# and workdate>#'+low+'#';
    -------------------------------------------------------------------
      s:=mStr + s + 'workdate<#'+high+'# and workdate>#'+low+'#';  s:=mStr + s + 'workdate<'''+high+''' and workdate>'''+low+'''';
    这两句的异同之处是什么?谢谢!!!
      

  13.   

    sql server的时间要用单引号括起来;
    access的时间要用#括起来,仅此而已。