Link to home
Start Free TrialLog in
Avatar of Bosanac
Bosanac

asked on

Check if record exists

I am entering new record directly into the ADOTable1 (in the grid)
using dbnavigator for inserting/posting/editing etc..
How can I have a message displayed if I enter a record that is allready in the database
as soon as I try move to move to the next field ?
ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bosanac
Bosanac

ASKER

it seems i can only trap in on before post event.
how is that supposed to look like
Use within your datasource the event OnUpdateData

  if YourField = '' then
  begin
    MessageDLG('Field value needs some input here....', mtWarning, [mbOK], 0);
    with YourEditField do begin Show; SetFocus; end;
    Abort;
  end;
  etc....
Use the OnBeforePost event in your ADOTable, IE.

procedure TForm1.MyADOTable1BeforePost(DataSet: TDataSet);
begin
if MyADOTable.Locate('MyFieldName', TextToFindInMyFieldName, [loCaseInsensitive] then
 begin
 {Do what you need to do...}
 end;

end;
Avatar of Bosanac

ASKER

whats 'TextToFindInMyFieldName' ?


If you are trying to find "Smith" in your field name 'LastName' then "Smith" would be the TextToFindInMyFieldName, which in the example is LastName.

This way you will find a record if it exists whose last name is Smith.
Avatar of Bosanac

ASKER

I am entering data into the grid directly and I do not know what record to search but to check the one I am entering (so I do not enter a duplicate record by any chance).
Is there a way..or perhaps before post or something ?
Place your validation before post on the OnUpdateData event on your datasource.

if YourCheckHere then
  begin
    MessageDLG('This is not what you want...', mtWarning, [mbOK], 0);
    with YourEditField do begin Show; SetFocus; end;
    Abort;
  end;
  etc, continue your check's here...
Forced accept.

Computer101
EE Admin