I have a table, 'Master' in which I keep the current value of the
primary key fields for several tables which use primary keys.
When I post a record to a table having a primary key I ask
the Master table to increment the key for that table and
give me the newly incremented value. The following routine
is intended to do that by providing the name of the field to
be incremented. However the line
FieldByName( Key ).AsInteger = Inc( nValue, 1 );
gives an error. Of course 'Key' is the name of the field.
How can I alter this code to make it work?
function GetKey( Key: String ): Integer;
var
mQuery: TFDQuery;
nValue: Integer;
begin
mQuery := TFDQuery.Create( AConnection );
with mQuery do
begin
Connection := AConnection;
SQL.Add( 'Select * from Master' );
Open;
nValue := FieldByName( Key ).AsInteger;
try
Edit;
FieldByName( Key ).AsInteger := Inc( nValue, 1 );
finally
Post;
Result := FieldByName( Key ).AsInteger;
end;
Free;
end;
end;
Thanks,
Jim Sawyer
Connect with Us