cannot perform this operation on a open/closed dataset?

RaymondSWalters picture RaymondSWalters · Oct 8, 2015 · Viewed 9.4k times · Source

Hi guys i've written a function which has to check the ID given to the function as iID and then output the name of the member of it is found, otherwise it must output that it is not found

A different table is active when this function is called so it must change the table to Members (to search the ID) and then back again afterwards (I have multiple tables)

function fCheckID(iID:integer):String;
var sTable:string;
begin
sTable:=datamoduleX.tableX.TableName;
datamoduleX.tableX.TableName:='Members';
 if datamoduleX.tableX.Locate('RefNo',iID,[]) then
  result:=dmRooiX.tblRooiX['Name']+' '+datamoduleX.tableX['Surname']
  else
  result:='ID: '+inttostr(iID)+' does not exist';
datamoduleX.tableX.TableName:=sTable;
end;

but the problem is every time I call this function I get an error that says "Cannot perform this operation on an open dataset"

if I close the dataset before I run the function I get "Cannot perform this operation on a closed dataset"

I know the error occurs when I try to access the table name or change it (the function does not give the error when those 3 lines are commented out)

I have no idea how to make this work any help would be greatly appreciated

Answer

Val Marinov picture Val Marinov · Oct 8, 2015

Example :

  Table1.TableName := 'TABLE1';
  Table1.Open;  
  Table1.TableName := 'TABLE2';  <-- Cannot perform this operation on Open data set. Because Table1 is open
  Table1.Locate('ID',11,[]);

simple solution

 Table1.TableName := 'TABLE1';
 Table1.Open;
 Table1.Close; <--Close table before change table name
 Table1.TableName := 'TABLE2';
 Table1.Open;  <-- Open new table before do Locate
 Table1.Locate('ID',11,[]);