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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check field

Status
Not open for further replies.

fule12

Programmer
Nov 12, 2001
140
YU
Hi All,
One beginner question.
How to check if data entered in field is unique ?
I create form for registering new Product.
So how to check for duplicate product names ?
I use Delphi 7 and Interbase.
Thanks


Fule
 
You can ensure that the field is stipulated as indexed no duplicates (or make the name your primary key, but this is not really advisable) , if you try and post your record an exception will be raised. A try except block will allow you to catch this and inform the user.

Another way is to use the locate method of a TDataset linked to your table, this searches for a record with the parameters you supply.
 
neildodsworth said:
Another way is to use the locate method of a TDataset linked to your table, this searches for a record with the parameters you supply.

The problem with this, if you're using data-aware controls is that as soon as you move the record pointer off the record that's being added or edited, it will post the data.

I have two possible solutions depending on whether you're using a client/server (MS SQL, Oracle, etc.) or desktop (Access, Paradox, xBase, etc.) database.

1. Client/server - Do a query like this:
Code:
select 1
from Table
where Field1 = :New_Data
Replacing "Table" with your table name, "Field1" with the name of the field you're trying to chect, and setting the "New_Data" parameter to the value you're trying to check. This will also work for desktop databases where the table you're looking at doesn't have a lot of data in it.

2. Desktop DB - Have an additional table object (I usually call it ttValidate) that is connected to the same table as the one you're editing. Use the FindKey (if the field is in an index) or Locate (if the field is not in an index) methods of the dataset to see if the value already exists.

-D
 
Thanks,
Hilfy, where I need to paste your code ?

select 1
from Table
where Field1 = :New_Data

On Event of field , maybe onExit Event ?

Sorry but i'm newbie in Delphi programing.

Thanks


Fule
 
The code your pasting is looking for a record that matches the information your user has entered, so you will need to do this prior to posting the change to the database and obviously after the user has enetered the data.

If your using a non data aware component you could possibly use the OnChange or OnExit event, in a dataware component you will need to use the BeforePost event of the TDataSet..

Hope that helps.

If it doesn't post a little more information about what method the user is using to enter the data... i.e. DBEdit etc....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top