Hi everybody,
I'm new programming with Delphi and all the object oriented stuff. And with databases.
I have a visual dBase database, I'm using TTable, TDataSource, TQuery.
My problem: there is a REFERENCE field that must be unique. REFERENCE is an index, I put it as manteined, but not unique, because it somehow compared the value of the field with the erased records of the table.
I wrote this code, with the help of a friend, to avoid records with repeted REFRENCE of been posted to the database:
begin
refActual:=DataSet.FieldByname('REFERENCE').AsString;
if buscaREFsalvar.Active then buscaREFsalvar.Close;
buscaREFsalvar.SQL.Clear;
buscaREFsalvar.SQL.Add('SELECT COUNT(*) AS TOT_REC FROM "'+ dbfPath +'" WHERE REFERENCE='''+refActual+'''');
try
buscaREFsalvar.active:=true;
except
On E: Exception do
begin
MessageDlg(E.Message, mtError, [mbOk], 0);
Abort;
Exit;
end;
end;
if buscaREFsalvar.FieldByName('TOT_REC').AsInteger > 0 then
begin
MessageDlg('Reference field must be unique', mtWarning, [mbOk], 0);
buscaREFsalvar.Close;
Abort;
Exit;
end;
buscaREFsalvar.Close;
end;
Now, the problem is that sometimes I want to edit an old record of my table, and I can't. I was thinking of change the query not to count the records, but to return the RecNo of the record where the query finds refActual. if the number of the records is the same, the record can be posted, but i don't know if it's gonna be posted only once or twice...
I hope someone can help...
Thanx in advance
I'm new programming with Delphi and all the object oriented stuff. And with databases.
I have a visual dBase database, I'm using TTable, TDataSource, TQuery.
My problem: there is a REFERENCE field that must be unique. REFERENCE is an index, I put it as manteined, but not unique, because it somehow compared the value of the field with the erased records of the table.
I wrote this code, with the help of a friend, to avoid records with repeted REFRENCE of been posted to the database:
begin
refActual:=DataSet.FieldByname('REFERENCE').AsString;
if buscaREFsalvar.Active then buscaREFsalvar.Close;
buscaREFsalvar.SQL.Clear;
buscaREFsalvar.SQL.Add('SELECT COUNT(*) AS TOT_REC FROM "'+ dbfPath +'" WHERE REFERENCE='''+refActual+'''');
try
buscaREFsalvar.active:=true;
except
On E: Exception do
begin
MessageDlg(E.Message, mtError, [mbOk], 0);
Abort;
Exit;
end;
end;
if buscaREFsalvar.FieldByName('TOT_REC').AsInteger > 0 then
begin
MessageDlg('Reference field must be unique', mtWarning, [mbOk], 0);
buscaREFsalvar.Close;
Abort;
Exit;
end;
buscaREFsalvar.Close;
end;
Now, the problem is that sometimes I want to edit an old record of my table, and I can't. I was thinking of change the query not to count the records, but to return the RecNo of the record where the query finds refActual. if the number of the records is the same, the record can be posted, but i don't know if it's gonna be posted only once or twice...
I hope someone can help...
Thanx in advance