User Management for an Internet Access Control Application - User Management Module Explained
(Page 3 of 4 )
This module does three things. First, it prepares the form for user input, and then it checks that all the fields in the form is updated. The newly inputted data is then posted to the database and a confirmation message is displayed, stating that a new user has been created. Let’s look at the code that makes each of these three steps happen.
The first procedure checks that the administrator entered all the required information:
procedure TForm6.BitBtn1Click(Sender: TObject);
begin
The first part of the code checks that the administrator entered all the required information. This information comprises the user's name, password, etc.:
if (edname.Text = '') OR (eduname.Text = '') OR (edupass.Text = '') OR (edupass2.Text = '') OR (edupass.Text <> edupass2.Text) then begin
If the administrator leaves out any of the information, an error message is displayed informing her of it:
messagedlg('Please ensure that you fill in all the fields and that the password fields are the same.',mtInformation,[mbOK],0);
exit;
end
If the administrator has entered all the information correctly, it is posted to the database using the insert property of the ado component:
else begin
ado1.TableName:='users';
ado1.Active := True;
ado1.Insert;
ado1.FieldByName('name').AsString := edname.Text;
ado1.FieldByName('username').AsString :=eduname.text;
ado1.FieldByName('password').AsString := edupass.text;
ado1.FieldByName('Alevel').AsString :=cb.Text;
ado1.Post;
end;
end;
A confirmation message is then displayed as the final step in this process:
procedure TForm6.ado1AfterPost(DataSet: TDataSet);
begin
messagedlg(edname.Text + ' has now been created.',mtinformation,[mbOK],0);
end;
_html_m6354c935.png)
The next option in the user management module covers changing a staff member's password in the database. The code should be familiar to you by now. The following takes place in this module: first, all the names of the users in the database are retrieved and stored in a combo box. You have to select the name of the user whose details you want to change, and then add the new fields to the form. The changes are posted to the database and a confirmation message is displayed. Below is the code for the module:
unit changepassword;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm7 = class(TForm)
gb1: TGroupBox;
cb: TComboBox;
StaticText1: TStaticText;
newpass: TEdit;
StaticText3: TStaticText;
StaticText4: TStaticText;
newpass2: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure cbSelect(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form7: TForm7;
pname:string;
implementation
{$R *.dfm}
uses Logon;
procedure TForm7.BitBtn2Click(Sender: TObject);
begin
close;
end;
procedure TForm7.FormShow(Sender: TObject);
begin
gb1.Enabled:=false;
form3.q.close;
form3.q.SQL.Text:='SELECT name from users';
form3.q.Open;
while not form3.q.Eof do begin
cb.Items.Add(form3.q.fieldbyname('name').AsString);
form3.q.Next;
end;
end;
procedure TForm7.cbSelect(Sender: TObject);
begin
pname:=cb.Text;
gb1.Enabled:=true;
end;
procedure TForm7.BitBtn1Click(Sender: TObject);
begin
//check to see that all password fields are filled in
if (newpass.Text = '') OR (newpass2.Text = '' ) OR (newpass.Text <> newpass2.Text) then begin
messagedlg('Please ensure that you fill in all the fields and that both new password fields are the same.',mtInformation,[mbOK],0);
exit;
end else
begin
form3.q.close;
form3.q.SQL.text:='INSERT INTO users SET password=:newpass WHERE name=:thename';
form3.q.Parameters.ParamByName('thename').Value:=pname;
form3.q.Parameters.ParamByName('newpass').Value:=newpass.text;
form3.q.ExecSQL;
messagedlg('The password for ' + pname + 'has been changed.',mtInformation,[mbOK],0);
end;
end;
end.
_html_m2aa4a55.png)
Next: Stats Page >>
More Delphi-Kylix Articles
More By David Web