I can't get the right primary key from MS-ACCESS using SQL command Select from query.
When i use a query that returns the last Auto number generated by access, sometimes it just returns a specific value, and not the actual LAST inserted auto number.
the problem persist until i repair the database.
The solution that i found was: when the auto number value isn't correct I try to "Compact and Repair the Database".
It solves the issue, and when i re-run my query it returns the last inserted value correctly.
But i can't be repairing the database every time that happens.
function GETID (): integer;
var
id: integer;
begin
query1.SQL.Clear;
query1.SQL.Add('INSERT INTO TABLE1 (CLIENTE,PEDIDO) values (0,0)');
query1.ExecSQL;
query1.SQL.Clear;
query1.SQL.Add('select * from table1');
query1.open;
query1.last;
id:=query1.Fields.Fields[0].AsInteger;
return id;
end;
As shown in the code the variable ID should return the last auto number value from the TABLE1.
Sometimes it does, sometimes it get stuck in some random number
Example Last ID in the table: 120; run the function: inserts a row with success in the table and now has as last id 121; the function should return: 121; it returns 120;
run the function again: inserts a row with success in the table and now has as last id 122; the function should return: 122; it returns 120;
The issue maybe related to a internal communication between access and the SQL, because the table inside the database is correct, and show the right number of rows and auto numbers, but when i try to get those values via sql it just doesn't get it right.
Is there any way to avoid this recurring issue? in other words avoiding corrupting those internal auto numbers references?
Thanks.