devxlogo

Key violation in networked Paradox table

Key violation in networked Paradox table

Question:
I have installed a Delphi 2.0 BDE application over a network where two terminals can access the table at the same time.

I have checked the NET DIR to point to shared directory on both terminals. However, after several operations, the application complains ‘KEY VIOLATION’.

I have suspected that the problem is caused by an autoincrement field(+) because if I delete the field and then re-create it, it goes back to normal.

What should I do? Make my own AutoIncrement procedure?

Answer:
That’s a really strange problem. AutoIncrement fields are “supposed” to be unique because there’s internal error checking in the BDE to make sure thereare no duplicates, but you never know. As to making your ownAutoIncrement procedure, that’s exactly what I do, primarily because Iwant total control of my data. AutoIncrement fields, while convenient, taketoo much control away. But I tend to be paranoid about that stuff.

The typical methodology for creating your own keys is to create a separate table that has a single field of type integer or float (depending uponyour needs), and a single row. Here’s a function that gets the key from a keytable:

function GetKey(KeyTblName, KeyFldName : String) : LongInt;var  tblKey : TTable;begin  tblKey := TTable.Create(Application);  with tblKey do begin    Active := False;    repeat { until successful or Cancel button is pressed }      try        Exclusive := True; { See if it will open }        Active := True;        Break;      except on EDatabaseError        {You can put code in here to abort if you want.         Otherwise, this will loop until it can get exclusive access.}      end;    until False;            Edit;    Result := FieldByName(KeyFldName).AsInteger;    Result := Result + 1;    FieldByName(KeyFldName).AsInteger := Result;    DbiSaveChanges(Handle);    Free;  end;end;
This function will increment the key value in the table and return the result, which you can add to your application. For example, here’s how you’duse it:
procedure AddNewRecord;begin  with Table1 do begin    if (State <> dsEdit) then      Edit;    Append;    FieldByName(‘MyKeyField’).AsInteger := GetKey(‘KeyTable.DB’, ‘KeyFld’);  end;end;
The disadvantage of this is that you have to make sure that you don’tduplicate keys in your application. But then again, you have total controlover how this works.

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist