?
Solved

Check if record exists

Posted on 2008-01-28
11
Medium Priority
?
296 Views
Last Modified: 2013-11-23
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 ?
0
Comment
Question by:Bosanac
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 21

Accepted Solution

by:
developmentguru earned 100 total points
ID: 20758463
 First you have to figure out how you identify a record that matches.  Is it just that the first and last name fields match?  Social Security Number?  Once you know how to identify a macthing record you can try a couple of different ways.

   Because you are entering records directly to a table you can use a unique index.  This way you will get an exception if the user attempts to enter duplicate data and the record will not save.  This is the least work for you.

  Another option I have seen used is to use OnBeforePost combined with a background table scan (the easy way to do this is to open another TTable and scan it).  

  Another option would be to use a separate entry screen (with non DB aware components) to enter the data.  When the user tries to save it, simply compare first.

  I'm sure there are many other ways to accomplish this, let me know if you need more detail on any of these.
0
 
LVL 18

Assisted Solution

by:Johnjces
Johnjces earned 100 total points
ID: 20759157
In that field's OnExit Event add the following:

if MyADOTable.Locate('MyFieldName', TextToFindInMyFieldName, [loCaseInsensitive] then
     begin
       {Do what you need to do...}
     end;

Would look something like:

Options are
loCaseInsensitive
loPartialKey
or none, i.e.; []

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

Open in new window

0
 

Author Comment

by:Bosanac
ID: 20766763
it seems i can only trap in on before post event.
how is that supposed to look like
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 4

Expert Comment

by:JeePeeTee
ID: 20767684
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....
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20767830
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;
0
 

Author Comment

by:Bosanac
ID: 20769483
whats 'TextToFindInMyFieldName' ?


0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20770175
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.
0
 

Author Comment

by:Bosanac
ID: 20804813
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 ?
0
 
LVL 4

Expert Comment

by:JeePeeTee
ID: 20812609
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...
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21186030
Forced accept.

Computer101
EE Admin
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question