在系统公共单元里面定义一个Record 或者 Class;
type TUserInfo:= Class
          .......
        public
          UserName:String;
          Area:String;
          PurView:String;
          function InitUserInfo:Boolean;
     end;
........................
Function TUserInfo.InitUserInfo:Boolean;
var
  i:integer;
begin
 .........
 //此处查询处相关登陆时候的用户信息。假设结果集保存在Query中。
  UserName:=Query.FieldByName('UserName').AsString;
  Area:=Query.FieldByName('Area').AsString;
  PurView:=Query.FieldByName('PurView').AsString;
end;//以后查询的时候就在每一句后面添加一个: and area = "'+ UserInfo.Area +'"'

解决方案 »

  1.   

    大花脸的主义不错啊,反正每个sql语句你都要加上where
      

  2.   

    登录时取到AREA值不就结了吗?
      

  3.   

    喔,我试试 bigysw(大花脸) 在方法。不成再问。
      

  4.   

    不成啊。大家再帮我看看。我做一个很简单的程序,大家帮我改改。
    _________________________________________________________________
    //form1里的代码
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Mask;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        MaskEdit1: TMaskEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
    if edit1.Text='' then
      begin
        ShowMessage('用户名不能为空!');
        edit1.SetFocus;
        exit;
      end;
        if maskedit1.Text='' then
        begin
          ShowMessage('密码不能为空!');
          maskedit1.SetFocus;
          exit;
        end
        else
        begin
        adoquery1.close;
        adoquery1.SQL.Clear;
        adoquery1.SQL.Add('select username,password,purview from sysuser');
        adoquery1.SQL.Add('where username='''+edit1.text+'''');
        adoquery1.sql.add('and password='''+maskedit1.Text+'''');
        adoquery1.Open;
        if (edit1.text<>adoquery1.FieldValues['username']) and (maskedit1.Text<>adoquery1.FieldValues['password']) then
        begin
        showmessage('用户名或密码错误');
        edit1.SetFocus;
        end
        else
        begin
        if adoquery1.fieldvalues['purview']='sys' then
        begin
        form2.dbgrid1.readonly:=fasle;
        ShowMessage('登陆成功,请选择菜单进行相应操作!');
        form2.show
        end;
        if adoquery1.FieldValues['purview']='user' then
        begin
        form2.dbgrid1.readonly:=true;
        ShowMessage('登陆成功,请选择菜单进行相应操作!');
        form2.show
        end;
        end;
        end;
        end;end.___________________________________________________________________
    //下边是Form2里的代码unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        ADOQuery1: TADOQuery;
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
     adoquery1.SQL.Clear;
     adoquery1.SQL.Add('select * from sysaccounts');
     adoquery1.SQL.Add('where area='''+rep+'''');
     adoquery1.open
    end;
    end.
    ___________________________________________________________________我想要一开始登陆时把用户的地区传给变量rep,然后再其他的FORM里查询时可以用where限制查询区域。我要怎么改才能让FORM2里的查询语句成立?
    表格请看我的第一贴。
      

  5.   

    //Unit2中修改
    //....
    var
      Form2: TForm2;
      rep: string;implementation
    //....//Unit1中修改
        adoquery1.Open;
        rep := adoquery1.FieldByName('area').AsString;
      

  6.   

    您可以这样做:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Mask;
    ...
    end;var
      Form1: TForm1;
      rep : String;implementation
    Uses
      Unit2;
    ...
    if adoquery1.FieldValues['purview']='user' then
        begin
        form2.dbgrid1.readonly:=true;
        ShowMessage('登陆成功,请选择菜单进行相应操作!');
        rep := ...//给登陆的地区值;
        form2.show//为什么不是动态的?
        end;
    ...
    =====
    Unit2
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Unit1,//要Unit1;
      Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        ADOQuery1: TADOQuery;
        DataSource1: TDataSource;
        DBGrid1: TDBGrid;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
     adoquery1.SQL.Clear;
     adoquery1.SQL.Add('select * from sysaccounts');
     adoquery1.SQL.Add('where area = :area');
     adoquery1.paramter.items[0].asstring := rep;
     adoquery1.open
    end;
    end.
    ___________________________________________________________________
      

  7.   

    喂,老大,你又抢我的分啊?
    为什么?
    完了,又没有分挣了;
    算了吧,给楼主提点意见;
    你看啊,你的登录窗体作的不太好啊,如果一但我再让登录窗体获得了焦点的话,你如何办啊?你的登录窗体现在是主窗体,关也关不成,你还不如让它hide或者是在.pro中进行调用;不要让它成主窗体了;
    CTRL+SHIFT+F11里改了;
    还有你的窗体名也应该换一换;
      

  8.   

    是,这个是我一个试区域的窗口,不是程序里的那个,这个目地是测试AREA。
      

  9.   

    再回来看看。你最好在一个公共模块:比如 DataMoudle 里面定义那个类。定义一个TUserInfo 类型的全局变量(相信你在很多地方都要用到登陆信息吧),在登陆成功后把当前登陆的用户信息保存进去。你在另外模块里面调用它就很方便了,这在项目以后的维护方面很重要。