Topic: Check duplicates before saving with combobox
Hi MVDB fans,
I am looking for the proper syntax to check if IDs are already in use when selecting them from Combobox and before clicking on "save" to make sense.
Current working code to check when clicking on save:
//##################################################################################################
//#
//# Check duplicate function
//#
//##################################################################################################
function CheckDuplicate (Action, sTable, sField, sValue: string; id: integer;): boolean;
var
s: string;
begin
result := False;
if Action = 'NewRecord' then
begin
s := SQLExecute ('SELECT Count(*) FROM '+sTable+' WHERE '+sField+' LIKE "' + sValue + '"');
if StrToInt(s) > 0 then result := True; //check if value exists
end;
if Action = 'ShowRecord' then
begin
s := SQLExecute ('SELECT Count(*) FROM '+sTable+' WHERE ('+sField+' LIKE "' + sValue + '") AND (id <> '+ IntToStr(id) +')');
if StrToInt(s) > 0 then result := True;
end;
end;
// Check if SIMNr id already used in Users table
procedure Details_ButtonSaveDet_OnClick (Sender: string; var Cancel: boolean);
begin
if CheckDuplicate(Details.dbAction, 'Users', 'id_SIM', Details.ComboSimNr.SQLValue, Details.ButtonSaveDet.dbGeneralTableId)
then
begin
MessageDlg('This SIM number is already in use.', mtError, mbOk, 0); // Displays a red 'X', header is: Error
Cancel := True;
end;
end;