Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Relational Databases
Expertise: Beginner
May 9, 1997

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 there are no duplicates, but you never know. As to making your own AutoIncrement procedure, that's exactly what I do, primarily because I want total control of my data. AutoIncrement fields, while convenient, take too 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 upon your needs), and a single row. Here's a function that gets the key from a key table:

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'd use 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't duplicate keys in your application. But then again, you have total control over how this works.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date