kwadratens
kwadratens

Reputation: 345

Delphi Paradox database - making duplicate of record | Key violation error

I need to import data into database of old invoicing program. The technology seems archaic, but I'm not discouraged and maybe someone will tell me a solution.

I created from components in Delphi 2005 (I have access to it and I found some documentation about it for Paradox) TTable (name: ParadoxTable), TDataSource (ParadoxDataSource) and TDBGrid, everything binded and pointed to the correct database after running the command ParadoxTable.Active := True; in TDBGrid everything is displayed correctly. I can add a record manually, store record and so on. But when I try to make it with code, I get a message every time

Key violation. Index: DPN_UNQ

but I do not have such a name in the table header (the first unique column is DPN_ID).

After extracting from the forgotten forum code, I added a line containing the assignment of a unique number just in case (moreover, without this ruler the effect is the same):

  ParadoxTable.disablecontrols;
  // Create a variant Array
  aField := VarArrayCreate(
               [0,ParadoxTable.Fieldcount-1],
                             VarVariant);
  // read values into the array
  for i := 0 to (ParadoxTable.Fieldcount-1) do
  begin
     aField[i] := ParadoxTable.fields[i].Value;
  end;
  aField[0] := '5'; // this added line of course changed nothing

  ParadoxTable.AutoCalcFields := True;
  ParadoxTable.Append;
  // Put array values into new the record
  for i := 0 to (ParadoxTable.Fieldcount-1) do
  begin
     if (ParadoxTable.fields[i].CanModify) then 
       ParadoxTable.fields[i].Value := aField[i];
  end;
  ParadoxTable.Post; // here i got exception error
  ParadoxTable.enablecontrols;

As a result, as in the attached image, a record is added (when i ommit line with ParadoxTable.Post;), no unique number is added, and all attempts to confirm and accept this record manually trigger the key violation error and in no way can I assign a value to the first variable / column.

effect of table i've tried also

  for i := 0 to (ParadoxTable.Fieldcount-1) do
  begin
    if  ParadoxTable.fields[i].Name<> 'DPN_ID' then
     if (ParadoxTable.fields[i].CanModify) then
       ParadoxTable.fields[i].Value := aField[i];
  end;

and also

for i := 1 to (ParadoxTable.Fieldcount-1) do // with omiting first [0] aField
  begin
    if  ParadoxTable.fields[i].Name<> 'DPN_ID' then
     if (ParadoxTable.fields[i].CanModify) then
       ParadoxTable.fields[i].Value := aField[i];
  end;

each time with the same effect. What am I doing wrong? Any other simple way? I passed all searches to the third Google page;) and no code worked properly. There are many such databases in the program to which I try to import them, which is why, for example, the issue of opening them from MS Acess falls out.

I have access to newer versions of Delphi / RAD if necessary, but I did not find the TTable component in them, on which all the information about the Paradox databases I found in the network was described. Maybe I should be interested in some other method at all? For example, maybe in newer versions of Delphi this is somehow more easily solved with other components?

Thank you in advance for guidance.

Upvotes: 0

Views: 1201

Answers (1)

A. I. Breveleri
A. I. Breveleri

Reputation: 767

Your code looks correct. The insertion is failing due to designed database constraints.

Possibly the table has a compound unique maintained secondary index named "DPN_UNQ" on fields "NAZWA" and "JM", or on the fields "DPN_ID", "DPN_UNQ", and "NAZWA". This would prevent the insertion of your fifth test record. Of course, if the index also includes other fields, then you can insert records by adding distinguishing values in those fields.

You need to know more about the structure of the table before you can know how to constrain your data for insertion.

Paradox databases are not required to contain schema tables. Some designers and DBAs will write their documentation into a INFORMATION_SCHEMA.db or some such, but the BDE will not examine this table for errors. Paradox databases generally tend to be useless unless properly documented outside the tables themselves.

As @zdzichs indicates, Delphi traditionally ships with a Database Desktop utility, which is basically a re-badged Paradox for Windows. With this utility you can examine the structure of any table, including the constraints and secondary indices.

Upvotes: 0

Related Questions