我主要想实现的是个简单的登陆问题
以前都用combobox做
现在不想用哪个了
但是每次都是输入第一条数据是对的
第二条数据数据就找不到
所以我放了个循环,结果出现了死循环
源程序如下:
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;type
  TForm2 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    ADOTable1: TADOTable;
    DataSource1: TDataSource;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;implementationuses Unit1;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
var
conn:string;
mypath:string;
mimi:string;
begin
conn:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+mypath+'data\student.mdb;Persist Security Info=False';
adotable1.ConnectionString:=conn;
adotable1.TableName:='admin';
adotable1.Open;
adotable1.First;
while not adotable1.Eof do
begin
if (trim(edit1.Text)=trim(adotable1.Fields[1].AsString)) and  (trim(edit2.Text)=trim(adotable1.Fields[2].AsString)) then
      begin
           form1.Show;
           adotable1.Next;
      end;
end;
   if edit1.Text='' then
   begin
      showmessage('用户名不能不填吧?!!');
      edit1.SetFocus;
   end
   else if edit2.Text='' then
          begin
            showmessage('密码忘了写!!');
            edit2.SetFocus;
          end
      else
 end;procedure TForm2.FormCreate(Sender: TObject);
begin
   edit1.Text:='';
   edit2.Text:='';
end;end.

解决方案 »

  1.   

    adotable1.First;
    while not adotable1.Eof do
    begin
    if (trim(edit1.Text)=trim(adotable1.Fields[1].AsString)) and  (trim(edit2.Text)=trim(adotable1.Fields[2].AsString)) then
          begin
               form1.Show;
               adotable1.Next;
          end;
    adotable1.next;
    end;
      

  2.   

    while not adotable1.Eof do
    begin
    if (trim(edit1.Text)=trim(adotable1.Fields[1].AsString)) and  (trim(edit2.Text)=trim(adotable1.Fields[2].AsString)) then
          begin
               form1.Show;
               adotable1.Next;///////去掉
          end;
     adotable1.Next;///////在这里加上
    end;
      

  3.   

    不好意思,上面的也是错的,应该是这样:
    while not adotable1.Eof do
    begin
    if (trim(edit1.Text)=trim(adotable1.Fields[1].AsString)) and  (trim(edit2.Text)=trim(adotable1.Fields[2].AsString)) then
          begin
               form1.Show;
               adotable1.Next;///////去掉
                break;
          end;
     adotable1.Next;///////在这里加上
    end;
      

  4.   

    1、先看这一段,作用是如果用户名和密码都正确就FORM1出现,对吧?
    那么
    while not adotable1.Eof do
    begin
      if (trim(edit1.Text)=trim(adotable1.Fields[1].AsString)) //这个TRIM我觉得不用加
      and (trim(edit2.Text)=trim(adotable1.Fields[2].AsString)) then  //这里是输入都
                                                                        正确了
       begin
          form1.Show;
          adotable1.Next;//干吗还要下一条记录?
       end;
    end;
    2、再看这一段
    if edit1.Text='' then
       begin
          showmessage('用户名不能不填吧?!!');
          edit1.SetFocus;
       end
       else if edit2.Text='' then
              begin
                showmessage('密码忘了写!!');
                edit2.SetFocus;
              end
          else
     end;
    按照逻辑上讲应该是先判断有没有空的,再判断是否正确吧?所以我觉得这么写好一点
    procedure TForm2.Button1Click(Sender: TObject);
    var
    conn:string;
    mypath:string;
    mimi:string;
    begin
       if edit1.Text='' then
       begin
          showmessage('用户名不能不填吧?!!');
          edit1.SetFocus;
          exit;
       end 
       else 
       if edit2.Text='' then
       begin
          showmessage('密码忘了写!!');
          edit2.SetFocus;
          exit;
       end
       else
       begin
          conn:='Provider=Microsoft.Jet.OLEDB.4.0;Data     
          Source='+mypath+'data\student.mdb;Persist Security Info=False';
          adotable1.ConnectionString:=conn;
          adotable1.TableName:='admin';
          adotable1.Open;
          adotable1.First;
          while not adotable1.Eof do
          begin
             if (trim(edit1.Text)=adotable1.Fields[1].AsString)
             and (trim(edit2.Text)=adotable1.Fields[2].AsString) then
             begin
                form1.Show;
             end 
             else
             adotable1.Next;
          end
          else
          showmessage('登陆失败');
       end;
    end;
    我没有调试,可能有些小问题自己调调吧。
      

  5.   

    错了     adotable1.Next;
          end
          else
          showmessage('登陆失败');
       end;
    这一段改成
         adotable1.Next;
         end;
       end
       else
       showmessage('登陆失败');
      

  6.   

    还是有问题啊
    showmessage('用户名不能不填吧?!!');
    showmessage('密码忘了写!!');
    因该放哪啊?
    虽然可以循环了
    但不管你的密码,用户名对不对
    他都要showmessage