Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking whether record is existing in database or not

Status
Not open for further replies.

beginner81

Programmer
Oct 27, 2003
44
MY
Can i know what's the code to check whether a record is exist in the database or not ??
The database server that i'm using is MsSQL .. and i had link it wif the following way :

TSQLConnection -> TSQLQuery -> TClientdataset

I's using TClientdataset to hold the data and update it to the database (once the user click at Update button)..

Anyway, i can't hav same record field (must b unique field eg CustID) in the database .. so i need to do the checkin
in the database instead of TClientdataSet (cos some of the time the TClientdataset data will b clear after it has been updates to database..
so instead of doin the checkin in TClientdataset b4 it has been updating to database, i need to do the checking in
Database.. so what code should i put ??

Here is the code that i's doin the checkin on TClientdataset..


Code for do the checking in TClientDataSet:
if NOT TClientDataSet.Locate('ProductID', [TEditProductID.Text], []) then // check whether it's in the TClientdataset or not
P/S:Something wrong wif the syntax i guess..


but all i want to do now
is to do the checking on Database instead of TClientdataset ... can anyone help me...


Thx in advance..
 
The problem you're having is that if a record is in edit or insert state, when you move the record pointer to another record (which is what your "Locate" does) it will autmatically post the record you've been working on. In order to check if the record already exists, you'll need to have a SEPARATE dataset that you can use to see if the record already exists. I recommend using a query that looks something like this:

Select 'X'
from MyTable
where KeyField1 = :KeyField1
and KeyField2 = :KeyField2

You'd then use something like the following code to see if the record already exists:

function TMyForm.RecordExists(Key1, Key2: String): Boolean;
begin
MyQuery.ParamByName('KeyField1').AsString := Key1;
MyQuery.ParamByName('KeyField2').AsString := Key2;
MyQuery.Open;
result := not MyQuery.IsEmpty;
MyQuery.Close;
end;

-D
 
It's working now mate.. thx a lot for ur help..it's appreciated..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top