1 (edited by wenchester21 2018-09-25 14:47:03)

Topic: Condition "IF" with SQL Query

In the action of clicking the "frmAddUser_btn_save" button, I declare some variables that auto followed assume their value of some objects of the "frmAddUser" form, others of an SQL query and other variables predefined in other methods.

A condition that has no complexity is also executed. The problem is the following:

When the variable "name_userVar" has value, everything works fine, when it is variable it has no value I skip the message I define but execute the SQL query that is inside the ELSE as well. This is what I do not want, I should not execute the SQL query.

var 
addtipoVarUS, passChangeVar  : String;
procedure frmAddUser_btn_save_OnClick (Sender: TObject; var Cancel: boolean);
var
userVar, nombre_userVar, readVar, writeVar, removeVar, searchVar, exportVar, printVar, dataVar, administratorVar, tipoVar : String;
fechaVar, tiempoVar : Extended;
begin
   writeVar := BoolToStr(frmAddUser.chbCreate.Checked);
   readVar := BoolToStr(frmAddUser.chbRead.Checked);
   removeVar := BoolToStr(frmAddUser.chbDelete.Checked);
   searchVar := BoolToStr(frmAddUser.chbSearch.Checked);
   exportVar := BoolToStr(frmAddUser.chbExport.Checked);
   printVar := BoolToStr(frmAddUser.chbPrint.Checked);
   dataVar := BoolToStr(frmAddUser.chbDatas.Checked);
   administratorVar := BoolToStr(frmAddUser.chbAdmin.Checked);

   userVar := SQLExecute('SELECT id FROM users WHERE (login = ''' + sUser + ''');');
   nombre_userVar := frmAddUser.edLogin.Text;
   tipoVar := addtipoVarUS;
   fechaVar := Now;
   tiempoVar := Time;

   if nombre_userVar = '' then
    begin
    ShowMessage('The "User" field can not be blank!!!');
    end else
    begin
    SQLExecute('INSERT INTO registros_user'+
               '(nombre_usuario, fecha, tiempo, tipo_registro, read, write, remove, search, export, print, data, administrator, pass, id_users) '+
               'VALUES("'+nombre_userVar+'","'+DateToStr(fechaVar)+'","'+TimeToStr(tiempoVar)+'","'+tipoVar+'","'+readVar+'","'+writeVar+'","'+removeVar+'","'+searchVar+'","'+exportVar+'","'+printVar+'","'+dataVar+'","'+administratorVar+'","'+passChangeVar+'","'+userVar+'")');
    UpdateDatabase('registros');
    UpdateDatabase('registros_user');
    end;
end;

Any idea of the problem ...

Re: Condition "IF" with SQL Query

It should works. But I see some strange keyword "elseCndition"


Please attach your project, I will test it.

Dmitry.

Re: Condition "IF" with SQL Query

DriveSoft wrote:

It should works. But I see some strange keyword "elseCndition"
Please attach your project, I will test it.

Those strange words that you see are a mistake, I'm going to remove them from the original post so there's no doubt.

Re: Condition "IF" with SQL Query

Just attach your project, I'll test it.

Dmitry.

Re: Condition "IF" with SQL Query

DriveSoft wrote:

Just attach your project, I'll test it.

When you try it you will see that it gives an error when saving the user, but that is not the error of which I speak, if you see that it can be, correct this new error

Post's attachments

Attachment icon Gest(ERROR).rar 12.61 kb, 725 downloads since 2018-09-25 

Re: Condition "IF" with SQL Query

When the variable "name_userVar" has value, everything works fine, when it is variable it has no value I skip the message I define but execute the SQL query that is inside the ELSE as well. This is what I do not want, I should not execute the SQL query.

You get an error message from button "Guardar" on Form "frmAddUser", not from your script.


Error message: 
NOT NULL constraint failed: users.login

Component: 
frmAddUser.btn_save

In the setting of this button set option "Notify user to fill required fields"

Dmitry.