Board index » delphi » Locate for Key Violation

Locate for Key Violation

Interbase , Delphi5 pro.,Win98

what is wrong with below codes ?

I have dataentry form which have few editboxes.
there is below code one of the editbox's on exit event to check key
violation.

cname:=edit1.text;
if  dm.IbQuery1.locate('CustName',cname,[]) then
begin
showmessage(' ..........');  // key violation
edit1.setfocus;
abort;
end;

it works well first time , but when i try to change the edit1.text (to
prevent key viol.) and exit from edit1.text 2.time my screen freezes.
for example,  edit1.text:= 'John Doe' but my table has another John Doe
to prevent the key violation i add a .(dot) at the end of the edit1.text
 John Doe.) after that  when i exit from edit1.text  my screen freeze.

2- my sql is  SELECT * FROM CUSTOMER ORDER BY CUSTNAME
(CustName has Unique Index on the table ) in this situation
which one is faster to locate a record ( on Query or on Table ? )
ibquery1.locate....  or  Ibtable1.locate.... ?

Thank You.

 

Re:Locate for Key Violation


This table has  150,000 records

Re:Locate for Key Violation


Your data validation code should be placed in the OnValidate event handler
for the dataset's field object. If the value is invalid raise an exception
and the user will not be allowed to move off of the field or post the
record.

Doing a SELECT of all records in a table from an SQL database server, such
as InterBase, is bad practice and will lead to very poor performance. Doing
a Locate on the result set of such a select is even worse because it causes
the entire 150,000 rows to be read from the server to the client. The normal
approach to designing client/server applications is to start the user with a
blank form and have them enter some selection criteria that will return a
small result set. When the user is done working with that set of records
repeat the process. When the user enters selection criteria use a SQL SELECT
statement to return only the rows that match the user's criteria. If you are
returning more than a hundred rows or so you need to rethink your design.

--
Bill
(TeamB cannot answer questions received via email)

Re:Locate for Key Violation


Thank you for your reply.

I'm new at Interbase but i think i make a mistake
Creating Database or Index

because i created a paradox table (with the same fields&same records,
(150,000 records) ) then tried to locate a record it took only 1 second to
locate the record which i'm looking for.

I created Database as below
CREATE DATABASE "C:\MYPROG\DATA\DATA.GDB" USER "SYSDBA" PASSWORD "masterkey"
PAGE_SIZE 8192
commit;

and then in database desktop i created the table (Customer)
CustName  ==>Unique Index
Adress
Tel_1
....
....

that is all , should i do something more regarding creating database or
index?

Thank you so much.

Re:Locate for Key Violation


Create a primary key for the table and add an index on the field that you
are searching on. Then use a SQL SELECT to find the record you want.

--
Bill
(TeamB cannot answer questions received via email)

Other Threads