Re: DeclareQueryVariables

J_Pygram

Member²
I am having trouble using this feature. Here is a sample of code where I get the error "variable :PERSON not declared"

TOracleQuery *Query=new TOracleQuery(NULL);
Query->Session=DBComp->MainSession;
Query->SQL->Add("select * from person");
Query->Execute();
TOracleDataSet *Person=new TOracleDataSet(this);
Person->Session=DBComp->MainSession;
Person->SQL->Add("select * from person");
Person->SQL->Add("where person=
tongue.gif
erson");
Person->DeclareQueryVariables(Query);
Person->SetVariable("person",8);
Person->Open();
Label1->Caption=Person->FieldByName("SURNAME")->AsString;
delete Person;
delete Query;

Any help would be appreciated.

Thanks
John
 
In the documentation, DeclareQueryVariables is mentioned in connection with OnApplyRecord, but it is probably safe to use in the more general manner that you seem to be attempting. However, you're basically turning things upside down.

DeclareQueryVariables is a helper function that takes a given dataset and analyzes the datatypes of all the fields (not variables) in it. With this starting point, it then defines the datatypes of the corresponding variables (not fields) in the query passed as a parameter. Correspondence is by name. It is basically shorthand for allowing you to skip lots of DeclareVariable calls in the situation where you have a dataset:
select (lots of variables) from (lots of tables) where (complicated join condition)
and you wish to update one of the tables in the from clause with:
update (table with a lot of columns) set (a list of a lot of columns with a lot of variables) where (some condition).
Without DeclareQueryVariables, you would need to seperately declare all the variables in the set list with DeclareVariable.
To make this long story short, the
tongue.gif
erson variable should be in the TQuery Query, and not in the TOracleDataSet Person.

Although it is not exactly clear what you are trying to achieve in your sample, the code as given could be boiled down to:
TOracleDataSet *Person=new TOracleDataSet(NULL /* using "this" is not necessary */);
Person->Session=DBComp->MainSession;
Person->SQL->Add("select person.rowid, person.* from person");
Person->SQL->Add("where person=
tongue.gif
erson");
Person->DeclareVariable("person",otInteger);
Person->SetVariable("person",8);
Person->Open();
Label1->Caption=Person->FieldByName("SURNAME")->AsString;
Person->Close();
delete Person;

The TOracleQuery is not necessary, as the use of rowid makes the dataset updatable.

Finally, you are doing something potentially dangerous that once drove me nuts since it worked on my home machine (NT), and failed miserably at work (95). I'm referring to Label1->Caption = "a string" at runtime. This won't always work. Basically, a TLabel is not a TWinControl, therefore it has no window and does not necessarily redraw itself as the result of a property change. Drawing the label is basically the responsibility of the parent, which may or may not get around to it sooner or later. You could probably work around this by doing an explicit Repaint() or Invalidate() following the setting of the caption, or consider the use of a TEdit or TStaticText.

HTH

------------------
Frans
 
Back
Top