Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't SET FILTER TO xx=thisform.xx

Status
Not open for further replies.

andreateh

Programmer
Jul 19, 2003
83
SG
I've create a form with 2 combo box.In this form i use only one table. And i set :

Code:
combobox.rowsource = cAlias.FieldA
combobox.rowsourceType = 6

in the combox1 valid method i write :

Code:
SELE cAlias
SET FILTER TO cAlias.Fielda = thisform.combo1.value

The problem is when i click on the combo2 or combo3, an error message come out..

THISFORM can only be use within a method

Can any one help me ?
 
andreateh

You need to use an object reference instead of THISFORM.

See faq184-4838 How can I create a simple forms manager?

for help on how to create object references.


Your code might look like :-

SET FILTER TO cAlias.Fielda = oForms.oMain.combo1.value



FAQ184-2483 - answering getting answered.​
Chris [pc2]
 
Thanks for help. But i saw an FAQ :

How to make an incremental search in Grid
faq184-1214

Code:
    SET FILTER TO ThisForm.cFilter $ &cFilterIn

Can run smooth with out problem ?
 
I would recommend against using any reference to the form in a filter setting:

You have to think of the scope of the variables referred to in a SET FILTER expression: In any context that that expression will be evaluated, all the referenced variables must be "in scope". Since all PRIVATE variables created in a procedure disappear when a procedure terminates, as well as all LOCAL's, only all the calling routines' PRIVATES, and PUBLICs remain. If you create a PUBLIC variable for the filter, it's likely never to get RELEASEd (since closing the table erases the SET FILTER, but doesn't erase the variable).

To avoid these problems, I'll often use macro replacement to create the filter expression, so that it only refers to literal values, not variables, ie:
Code:
lcFilterVal = thisform.combo1.value
SELECT cAlias
SET FILTER TO cAlias.Fielda = [&lcFilterVal]
 
To clarify: creating an object reference to the form would be creating a PUBLIC variable: If not cleaned up right, this public reference could prevent the form from releasing properly, and at the very least, could likely lead to conflicts with other instances of the same form running concurrently.

There are ways (some complicated, some more complicated) to solve these issues, or just simply use macro replacement and literal values in the filter instead of form references or variables.
 
wgcs's suggestion has the added benefit of improving performance for the filter. When doing operations in/on the filtered datasource (operations that respect the filter), and the filter expression contains an object/variable VFP reevaluates(checks) the object/variable over and over again...where using a a literal value VFP does not have to keep checking it.

boyd.gif

craig1442@mchsi.com
"Whom computers would destroy, they must first drive mad." - Anon​
 
wgcs said:
creating an object reference to the form would be creating a PUBLIC variable

It may be necessary to delare it be PUBLIC to avoid it going out of scope, but unless you implicitly declare it to be PUBLIC before the code
Code:
DO FORM form1 NAME oForm1
oForm1 will become PRIVATE by default and not a PUBLIC variable.

The critical thing about object references is to ensure they are destroyed with the object and never rely on any subsequent event to remove them.

FAQ184-2483 - answering getting answered.​
Chris [pc2]
 
Thanks for helps. Yesterday night i have found a way that quite similiar with WGS's. I try
Code:
lcFilter = "Set Filter to cAlias.FieldA ='"+ ; 
            thisform.combo1.value+"'"
SELECT cAlias
&lcFilter
Also Work fine.
 
Looks good: Just be carefull that the value in THISFORM.Combo1.Value never has an apostraphe in it, or else the filter will break (and throw a VFP error)... That was why I used the square brackets to delimit the string instead of quotes or single-quotes (apostraphes). User-entered strings are much less likely to contain the "]" character, which is the only character not allowed [within single brackets]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top