0

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

RaymondSWalters
  • 189
  • 3
  • 5
  • 12
  • 1
    Use a prepared Query for that kind of operations – Jens Borrisholt Oct 08 '15 at 09:29
  • I'm sorry but I don't know what you mean by that – RaymondSWalters Oct 08 '15 at 09:34
  • 2
    Your approach is completely wrong, imo: a) you seem unaware that you can have more than one table open at the same time; b) Don't change a table's TableName to access a different table in the database, use a separate Table component instead; c) You do a Locate on a dataset in one datamodule but then look data up from another table in a different datamodule. I think it would be best if you study a basic online Delphi db tutorial and start again. – MartynA Oct 08 '15 at 09:41
  • 2
    Close; Change Table name; Open. <- This is a very poor solution though .I would use a local query (via `TADOQuery`) for this task (e.g `select * from Members where RefNo=:iID)`, instead of abusing `datamoduleX.tableX` which returns **all** records each time. – kobik Oct 08 '15 at 09:43
  • @MartynA thanks for the help. I'm only still in high school so this is all new to me. I will try adding multiple tables – RaymondSWalters Oct 08 '15 at 09:46
  • No worries and good luck. See my comment on the answer you've just accepted. – MartynA Oct 08 '15 at 10:05

1 Answers1

2

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,[]);
Val Marinov
  • 2,705
  • 17
  • 22
  • 2
    Sorry, I think that for a beginner like the OP, using the same dataset component to access different datasets is only going to invite confusion and misunderstanding. Why not just use another TDataSet? I assume "IBTable1" is a typo? – MartynA Oct 08 '15 at 10:03
  • @MartynA I agree with you. I now have different table components with their own dat as ounces – RaymondSWalters Oct 08 '15 at 10:05
  • @MartynA Totally true. But this is just an example of where they come from error messages. – Val Marinov Oct 08 '15 at 10:12