login 代码:unit login;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;type
  TLoginFm = class(TForm)
    GroupBox1: TGroupBox;
    EdName: TEdit;
    EdPassword: TEdit;
    ButOk: TButton;
    ButCannel: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure EdNameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EdPasswordKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ButCannelClick(Sender: TObject);
    procedure ButOkClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  LoginFm: TLoginFm;varMaxtimes,Logintimes : integer ;implementationuses Dmdata, Poffice;
{$R *.dfm}
procedure TLoginFm.ButOkClick(Sender: TObject);
var Aname,Apass,Sqlstr:string;
begin    if (length(trim(EdName.text))>0) or (length(trim(EdPassword.text))>0) then
       begin
         Aname := trim(Edname.Text);
         Apass := trim(Edpassword.Text);
         Sqlstr := 'select * from loguser where (管理员='''+Aname+''') and (密码='''+Apass+''')';
       //with DataMod.QUser.Create(application) do
       // with DataMod.QUser.Create(nil) do
       //with DataMod.QUser do 
      with TADOQuery.Create(nil)do
         begin
           try
           Close;
           Connection:=Datamod.ADOConn;           Sql.Clear;
           sql.Add(sqlstr);
           open;
             if recordcount > 0 then
               begin                frmain.show;  //显示主程序               end
             else
               begin
                showmessage('密码是否正确');
                EdPassword.SetFocus;
                EdPassword.SelectAll;
               end;
           finally
           Free;
           end;
           end;
           end
      else
           begin
             showmessage('请确定名称和密码是否正确');
             Edname.SelectAll;
           end;
end;end.
===========================
工程program office;uses
  Forms,
  Dialogs,
  Poffice in 'Poffice.pas' {FrMain},
  Dmdata in 'Dmdata.pas' {DataMod: TDataModule},
  login in 'login.pas' {LoginFm};{$R *.res}begin
 Application.Initialize;  Application.CreateForm(TFrMain, FrMain);
  Application.CreateForm(TDataMod, DataMod);
  if not DataMod.ADOConn.Connected then
  begin
    ShowMessage('连接后台数据库失败,请检查配置是否完好,数据库是否存在');
    halt;
  end;
  Application.CreateForm(TLoginFm, LoginFm);
  LOGINfm.ShowModal;
  LOGINfm.close; Application.Run;
end.
想在运行时先执行 Fmlogin 登录界面, 当密码正确的时,则运行主程序 frmain,并且这个登录关闭,但上面的代码一运行,主程序能运行,但登录窗口还是存在,并且,关掉登录窗口后,主程序也被关掉了,希望高手指点,谢谢!

解决方案 »

  1.   

    if recordcount > 0 then
                   begin
                    Fmlogin.Close;
                    Fmlogin.Free;
                    frmain.show;  //显示主程序               end
      

  2.   

    yousite1(国雾)if recordcount > 0 then
                   begin
                    Fmlogin.Close;
                    Fmlogin.Free;
                    frmain.show;  //显示主程序               end
    ===========================按照你的方法,有错误出现!
      

  3.   

    1、你的LOGINFORM不要和MAINFORM耦合在一块。
    2、LOGINFORM中运用MODALRESULT这个值: 验证成功--》MODALRESULT := MrOk
                                         验证失败--》MODALRESULT := MrCancel
    3、执行顺序在Project中来设置(由LOGINFORM的MODALRESULT来决定是否继续运行)给你一段代码(我没有用DATANODULE),也许能帮上你  Application.Initialize;
      Application.Title := '管理系统';
      with TLoginForm.Create(nil) do
      begin
        if ShowModal = MrOk then
        begin
          Free;
          Application.CreateForm(TMainForm, MainForm);
          Application.CreateForm(TActionsModule, ActionsModule);
          Application.Run;
        end;
      end;