Stef Merlijn
asked on
How to delete the last record of a filtered ADO dataset.
Hi,
I have three TADOTables (1=TCompanies, 2=TClients, 3=TCompanyClients).
The last two tables are connected to the same DatabaseTable = Clients.
PROBLEM 1:
When I am on the company level and want to display a grid with the clients belonging to that company I use a filter on the TClients table.
When I an on the client level I use the Clients table without a filter.
When I delete the last record in the filtered dataset (TClients) and try to do a refresh of the table then I get an errormessage saying that the record is out of range (row changed, index bla bla bla).
PROBLEM 2:
The table TCompanyClients is only used to quickly determine if the company has any clients. It has the TCompany datasource assigned to it as the Mastersource.
If the selected company has at least one client then a menuitem "Show Clients of this company" is shown (else it will be hidden).
This table isn't updated correctly after I post or delete records into the TClients table. So the menuoption isn't correctly updated also.
Now I have solved this by closing and opening (in stead of refreshing) the TClients and TCompanyClients tables. But this is not a good solution as you can imagine.
************************** ********** ********** ********** *****
One remark: I don't want to change datasources on DBGrids, DBedits etc. on my form
************************** ********** ********** ********** *****
Please supply me with some code that will solve these two problems correctly.
Thanks a lot.
The Wiz
I have three TADOTables (1=TCompanies, 2=TClients, 3=TCompanyClients).
The last two tables are connected to the same DatabaseTable = Clients.
PROBLEM 1:
When I am on the company level and want to display a grid with the clients belonging to that company I use a filter on the TClients table.
When I an on the client level I use the Clients table without a filter.
When I delete the last record in the filtered dataset (TClients) and try to do a refresh of the table then I get an errormessage saying that the record is out of range (row changed, index bla bla bla).
PROBLEM 2:
The table TCompanyClients is only used to quickly determine if the company has any clients. It has the TCompany datasource assigned to it as the Mastersource.
If the selected company has at least one client then a menuitem "Show Clients of this company" is shown (else it will be hidden).
This table isn't updated correctly after I post or delete records into the TClients table. So the menuoption isn't correctly updated also.
Now I have solved this by closing and opening (in stead of refreshing) the TClients and TCompanyClients tables. But this is not a good solution as you can imagine.
**************************
One remark: I don't want to change datasources on DBGrids, DBedits etc. on my form
**************************
Please supply me with some code that will solve these two problems correctly.
Thanks a lot.
The Wiz
Would you like to use TADOQuery instead of TADOTable ?
This works for me:
procedure TForm1.adoqClientsNewRecor d(DataSet: TDataSet);
begin
adoqClients.FieldByName('C OMPANY_ID' ).AsIntege r := adoqCompanies.FieldByName( 'ID').AsIn teger;
end;
procedure TForm1.TClientsNewRecord(D ataSet: TDataSet);
begin
TClients.FieldByName('COMP ANY_ID').A sInteger := CompanyID;
end;
procedure TForm1.TCompaniesAfterScro ll(DataSet : TDataSet);
begin
CompanyID := TCompanies.FieldByName('ID ').AsInteg er;
TClients.Filtered := False;
TClients.Filter :=''
+ 'COMPANY_ID = '
+ IntToStr(TCompanies.FieldB yName('ID' ).AsIntege r)
+ '';
TClients.Filtered := True;
if TClients.Active then
TClients.Requery([]);
end;
procedure TForm1.TClientsAfterDelete (DataSet: TDataSet);
begin
TClients.Requery([]);
TCompanyClients.Requery([] );
end;
procedure TForm1.TClientsAfterPost(D ataSet: TDataSet);
begin
TCompanyClients.Requery([] );
end;
procedure TForm1.adoqClientsNewRecor
begin
adoqClients.FieldByName('C
end;
procedure TForm1.TClientsNewRecord(D
begin
TClients.FieldByName('COMP
end;
procedure TForm1.TCompaniesAfterScro
begin
CompanyID := TCompanies.FieldByName('ID
TClients.Filtered := False;
TClients.Filter :=''
+ 'COMPANY_ID = '
+ IntToStr(TCompanies.FieldB
+ '';
TClients.Filtered := True;
if TClients.Active then
TClients.Requery([]);
end;
procedure TForm1.TClientsAfterDelete
begin
TClients.Requery([]);
TCompanyClients.Requery([]
end;
procedure TForm1.TClientsAfterPost(D
begin
TCompanyClients.Requery([]
end;
Please remove:
procedure TForm1.adoqClientsNewRecor d(DataSet: TDataSet);
begin
adoqClients.FieldByName('C OMPANY_ID' ).AsIntege r := adoqCompanies.FieldByName( 'ID').AsIn teger;
end;
procedure TForm1.adoqClientsNewRecor
begin
adoqClients.FieldByName('C
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot. Again..... :-)
You are welcome Delphiwizard !
:-))
:-))
ASKER
I saw that you have added a description to your solutions.
That makes things a lot more easy to find.
...Stef
That makes things a lot more easy to find.
...Stef
Yes, it was your advice to post description as I remember :-))
Emil
Emil
ASKER
I have my moments.....