unit Unit7;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB;type
  TForm7 = class(TForm)
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Edit1: TEdit;
    CheckBox1: TCheckBox;
    Button1: TButton;
    ADODataSet1: TADODataSet;
    ADOConnection1: TADOConnection;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Label1: TLabel;    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  //object adodataset1:tadodataset
  //parameters=<>
  //end;
var
  Form7: TForm7;implementation{$R *.dfm}procedure TForm7.FormCreate(Sender: TObject);
begin
self.ComboBox1.ItemIndex:=0;
self.ComboBox2.ItemIndex :=0;end;procedure TForm7.Button1Click(Sender: TObject);
var
 str:string;
begin
ADODataSet1.Active:=false;
if checkbox1.Checked then
  begin
     if combobox1.Text='会员编号' then
     begin
    str:='select *from 借阅信息';
    str:=str+'where 借阅信息.会员编号';
    str:=str+combobox2.Text+edit1.Text ;
    end;
   if combobox1.Text='会员姓名' then
    begin
    str:='select *from 会员信息.姓名,借阅信息';
    str:=str+'会员信息 inner join 借阅信息';
    str:=str+'on 会员信息.会员编号=借阅信息.会员编号';
    str:=str+'where 会员信息.姓名' ;
    str:=str+combobox2.Text +''''+edit1.Text ;
    end;
  end
  else
  begin
   if combobox1.Text='会员编号' then
   begin
     str:='select *from 借阅信息 where 借阅信息.会员编号';
     str:=str+combobox2.Text + edit1.Text ;
     str:=str+'and 借阅信息.是否归还=''否''';
   end;
   if combobox1.Text ='会员姓名' then
     begin
      str :='select 会员信息.姓名,借阅信息*from 会员信息 inner join 借阅信息';
      str:=str+'where 会员信息.姓名';
      str:=str+combobox2.Text+''''+edit1.Text +'''';
      str:=str+'and 借阅信息.是否归还=''否''';
     end;
     end;
      ADODataSet1.CommandText:=str;
      ADODataSet1.Active:=True;
end;end.

解决方案 »

  1.   

    呵呵低级错误:
    str :='select 会员信息.姓名,借阅信息 from 会员信息 inner join 借阅信息';
          str:=str+'where 会员信息.姓名';注意分开啊!
    str :=' select 会员信息.姓名,借阅信息 from 会员信息 inner join 借阅信息 ';
          str:=str+' where 会员信息.姓名 ';
      

  2.   

    DELPHI给我们提示一个很好的环境,你可以设断点跟踪啊,一看就知道啦!
    :-)
      

  3.   

    procedure TForm7.Button1Click(Sender: TObject);
    var
     str:string;
    begin
    ADODataSet1.Active:=false;
    if checkbox1.Checked then
      begin
         if combobox1.Text='会员编号' then
         begin
        str:='select *from 借阅信息';
        str:=str+'where 借阅信息.会员编号';
        str:=str+''''+combobox2.Text+edit1.Text'''' ;
        end;
       if combobox1.Text='会员姓名' then
        begin
        str:='select *from 会员信息.姓名,借阅信息';
        str:=str+'会员信息 inner join 借阅信息';
        str:=str+'on 会员信息.会员编号=借阅信息.会员编号';
        str:=str+'where 会员信息.姓名' ;
        str:=str+''''+combobox2.Text +edit1.Text'''' ;
        end;
      end
      else
      begin
       if combobox1.Text='会员编号' then
       begin
         str:='select *from 借阅信息 where 借阅信息.会员编号';
         str:=str+combobox2.Text + edit1.Text ;
         str:=str+'and 借阅信息.是否归还=''否''';
       end;
       if combobox1.Text ='会员姓名' then
         begin
          str :='select 会员信息.姓名,借阅信息*from 会员信息 inner join 借阅信息';
          str:=str+'where 会员信息.姓名';
          str:=str+''''+combobox2.Text+edit1.Text +'''';
          str:=str+'and 借阅信息.是否归还=''否''';
         end;
         end;
          ADODataSet1.CommandText:=str;
          ADODataSet1.Active:=True;
      

  4.   

    if checkbox1.Checked then
      begin
         if combobox1.Text='会员编号' then
         begin
        str:='select * from 借阅信息';
        str:=str+' where 借阅信息.会员编号';
        str:=str+''''+combobox2.Text+edit1.Text +'''';
        end;
       if combobox1.Text='会员姓名' then
        begin
        str:='select a.* from 会员信息 as a,';
        str:=str+' inner join 借阅信息 ad b';
        str:=str+' on (a.会员编号=b.会员编号)';
        str:=str+'and (a.姓名' ;
        str:=str+combobox2.Text +''''+edit1.Text+''')' ;
        end;
      end
      else
      begin
       if combobox1.Text='会员编号' then
       begin
         str:='select * from 借阅信息 as a where a.会员编号';
         str:=str+combobox2.Text +''''+edit1.Text+'''' ;
         str:=str+'and a.是否归还=''否''';
       end;
       if combobox1.Text ='会员姓名' then
         begin
          str :='select 会员信息.姓名,借阅信息.* from 会员信息 inner join 借阅信息';
          str:=str+'on 会员信息.姓名';
          str:=str+combobox2.Text+''''+edit1.Text +'''';
          str:=str+'and 借阅信息.是否归还=''否''';
         end;
         end;
      

  5.   

    不如交你一个方法:var SQLText:String;
    ...
    if ...
    SQLText:='Select * from ... '//在这里用断点跟踪一下。
    //比如你下面可以改为
        str:='select a.* from 会员信息 as a,';
        str:=str+' inner join 借阅信息 ad b';
        str:=str+' on (a.会员编号=b.会员编号)';
        str:=str+'and (a.姓名' ;
        str:=str+combobox2.Text +''''+edit1.Text+''')' ;    str:= 'select a.* from 会员信息 as a,'+
              +' inner join 借阅信息 ad b'+
              ...
              +combobox2.Text +''','''+edit1.Text+'''';   便于跟踪。
    //close
    SQL.clear;
    SQL.add(SQLText);
    open;