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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.