在系统公共单元里面定义一个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 +'"'
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 +'"'
_________________________________________________________________
//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里的查询语句成立?
表格请看我的第一贴。
//....
var
Form2: TForm2;
rep: string;implementation
//....//Unit1中修改
adoquery1.Open;
rep := adoquery1.FieldByName('area').AsString;
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.
___________________________________________________________________
为什么?
完了,又没有分挣了;
算了吧,给楼主提点意见;
你看啊,你的登录窗体作的不太好啊,如果一但我再让登录窗体获得了焦点的话,你如何办啊?你的登录窗体现在是主窗体,关也关不成,你还不如让它hide或者是在.pro中进行调用;不要让它成主窗体了;
CTRL+SHIFT+F11里改了;
还有你的窗体名也应该换一换;