{ Get max key value}
function quGetMaxID_(tbName,FieldName: String): LongInt;
begin
with TQuery.Create(nil) do
try
DatabaseName := DBname;
SQL.Add('SELECT MAX('+FieldName+') FROM ' + QuotedStr(tbName));
Open;
result := Fields[0].AsInteger + 1;
finally
Close;
Free;
end;
end;
{ insert new record and return new ID value}
function quInsertBlankSQL_(tbName,fName: string; var id: Longint): boolean;
var
i: integer;
begin
Result := False;
for i:=1 to RepeateAccess do
begin
id := quGetMaxID_(tbName,fName);
Result := quInsertKeySQL_(tbName,fName,id);
if Result then
Break;
end;
end;
{ Insert record for ID}
function quInsertKeySQL_(tbName, KeyField: string;
KeyValue: Longint): boolean;
var
i: integer;
str: string;
begin
Result := False;
str := 'INSERT INTO '+tbName+' ('+ KeyField + ')'+
' VALUES ('+IntToStr(KeyValue)+')';
for i:=1 to gRptAccess do
begin
Result := quExecuteSQL_(str);
if Result then
Break;
end;
end;
function quExecuteSQL_(SQLstring: string): boolean;
begin
with quCreateTmp_(SQLstring) do
begin
try
ExecSQL;
Result := True;
except
on E: Exception do
begin
Result := False;
end;
end;
Free;
end;
end;
function quCreateTmp_(SQLstring: string): TQuery;
begin
Result:= TQuery.Create(nil);
with Result do
begin
DatabaseName := DBname;
SQL.Text := SQLString;
end;
end;
|