Hello casedemarcat,
It's a bit complicated to correct you script as I don't really known what it does.
What I can try to show you is how a script is organized.
-----> AT THE BEGINING OF THE SCRIPT
you can (but not always) find what we call global variables. It is not recommended, but if you can not avoid using them, they must be declared on the first lines like this :
var
Timer: TTimer;
iSeconds: integer;
No begin, end or procedure declarations, just VAR and then the variables you declare.
-----> WITHIN THE SCRIPT
this is where all your procedures and functions will be declared. More a less, a procedure DOES something while a function returns a RESULTS, but this is not always the case.
A procedure is declared like this :
procedure frmLogin_bLogin_OnClick (Sender: string; var Cancel: boolean);
begin
//what the procedure will do
end;
you must use the keyword procedure first, then give it a name (without spaces), then (eventually), parameters between ( and )
most of the time, this will be written my the Object Inspector when you when you double click in the desired event (onclick, onmousedown, onclose and so on).
begin and end; are there to fix the limits of the procedure, everything outside will be ignored (two exception that we will see later)
If you need some variables to make you procedure run, they are declared AFTER the procedure keyword, and BEFORE the begin statement like this :
procedure frmLogin_bLogin_OnClick (Sender: string; var Cancel: boolean);
var
i : Integer;
s : String;
begin
//what the procedure will do
end;
I'm making this clear because I found some VAR outside procedures in your script and that makes them global variables and your script is hard to read after that
I said they were exceptions to the fact that everything outside the begin/end is ignored.
The first exception is global variables : if you declared a global variable at the begining of your script like
then in the previous example of procedure you do not need to redeclare it, you can assign it right away like this :
procedure frmLogin_bLogin_OnClick (Sender: string; var Cancel: boolean);
var
i : Integer;
s : String;
begin
sUser := 'Mathias';
//what the procedure will do
end;
The second exception I can think of (there might be others) is when a procedure makes a call to another procedure, but that is another story.
-----> AT THE END OF THE SCRIPT
you will find this :
begin
//some script, but not always
end.
Did you notice that every instruction during your code ends with a ; when the last end at the end of the script is followed by a dot (.) ?
Everything you place AFTER that statement will be ignored.
What about the instructions between those tags ? Well, they are always executed everywhere.
For example, in my scripts, you will fond this :
begin
//---> Hints delays and duration
Application.HintHidePause := 15000; //How long will the hints be displayed
Application.HintPause := 300; //After how long will the hints appear
end.
With this, I am sure that on any Form of my application, if I set up a Hint, they will be displayed after 0,3 second of pause, and for 15 seconds (as long as the user does not move the mouse).
I also think that this is the first code executed by the program on startup (not sure), so if you want to check for the presence of users, you could put it here. I personally put this on the onshow event of Form1, because after it's been checked, I don't need it anymore. Personal taste.
-----> A FEW MORE THINGS
Somewhere at the begining of your script I found :
This is correct, as you declare the variable and assign it an empty string (which is not NULL but empty).
But this same variable is used in a following procedure like this :
begin
sUser := frmLogin.edUser.Text; // remember username
Why assign a value to a global variable and then assign it another value when you first use it ? Just declare it globally and the assign it a value when you use it. Just my opinion, you might have reasons I did not guess.
Also, I saw this :
begin
// SQL query for check user and password
s := VarToStr( SQLExecute('SELECT count(id) FROM users WHERE (login = ''' + frmLogin.edUser.Text + ''') AND (password = ''' + frmLogin.edPassword.Text + ''');') );
// if SQL query found login and password
if (s <> '0') and (s<>'') then
begin
sUser := frmLogin.edUser.Text; // remember username
// check admin rights
s := VarToStr( SQLExecute('SELECT administrator FROM users WHERE (login = ''' + sUser + ''');') );
if s='0' then Form1.bUsers.Enabled := False; // if the user does not have administrator privileges, deactivate the button to create other users
// check permissions to read
s := VarToStr( SQLExecute('SELECT read FROM users WHERE (login = ''' + sUser + ''');') );
if s='0' then Form1.bEdit.Enabled := False; // deactivate the button to edit the record
// check write permissions
s := VarToStr( SQLExecute('SELECT write FROM users WHERE (login = ''' + sUser + ''');') );
if s='0' then
begin
Form1.bAdd.Enabled := False; // deactivate the button to add the record
frmEmployee.bOK.Enabled := False; // deactivate the button to save the record
end;
// check permission to delete
s := VarToStr( SQLExecute('SELECT remove FROM users WHERE (login = ''' + sUser + ''');') );
if s='0' then Form1.bDelete.Enabled := False; // deactivate the button to delete the record
// check permission to search
s := VarToStr( SQLExecute('SELECT search FROM users WHERE (login = ''' + sUser + ''');') );
if s='0' then Form1.bSearch.Enabled := False; // deactivate the button to search the records
frmLogin.Close; // close login form
end else MessageDlg('Parola dvs nu este corecta', mtError, mbOk, 0); // message if the password is incorrect
end;
This is perfectly correct but you use 5 SQLQueries to check rights one after another when you could just use one like this :
procedure frmLogin_bLogin_OnClick (Sender: string; var Cancel: boolean);
var
s: string;
Rights : TDataSet;
begin
// SQL query for check user and password
s := VarToStr( SQLExecute('SELECT count(id) FROM users WHERE (login = ''' + frmLogin.edUser.Text + ''') AND (password = ''' + frmLogin.edPassword.Text + ''');') );
// if SQL query found login and password
if (s <> '0') and (s<>'') then
begin
sUser := frmLogin.edUser.Text; // remember username
SQLQuery('SELECT * FROM users WHERE login = "'+sUser+'"', Rights); //one query and all the results are assigned to the DataSet called Rights
while not Rights.Eof do //shuffle through the DataSet calling the fields by their names
begin
if Rights.FieldByName('administrator').AsInteger = 0 then Form1.bUsers.Enabled := False;
if Rights.FieldByName('read').AsInteger = 0 then Form1.bEdit.Enabled := False;
if Rights.FieldByName('write').AsInteger = 0 then
begin
Form1.bAdd.Enabled := False;
frmEmployee.bOK.Enabled := False;
end;
if Rights.FieldByName('remove').AsInteger = 0 then Form1.bDelete.Enabled := False;
if Rights.FieldByName('search').AsInteger = 0 then Form1.bSearch.Enabled := False;
Rights.Next;
end;
Rights.Free;
frmLogin.Close; // close login form
end
else MessageDlg('Parola dvs nu este corecta', mtError, mbOk, 0); // message if the password is incorrect
end;
code is untested, but you get the idea I hope.
That's what I can tell you for the moment. I know it might not be the solution you were expecting, but I hope it helps a bit.
Cheers
Mathias
I'm a very good housekeeper !
Each time I get a divorce, I keep the house
Zaza Gabor