Filter subform

Hi there.

This may seem like a silly and beginner question, but I haven’t found the answer anywhere.

I have a form, with a subform in it, and I need to filter a field on the subform (list), based on the value of another field in the main form (card).

For example, my current record in the main form has a field called “type”, and its value is ‘abc’, I want to filter the subform’s “type” field with the value ‘abc’. Only the lines whose “type” value is ‘abc’ should appear on the subform.

How do I do it?

Check SubFormLink propery in subform…

I did. But I haven’t been able to filter the subform.

In any case, i’d prefer to filter the subform via code, since I need to set another condition, in which the filter depends on the value of another field in the form…

You need to try by adding filter code to subform records in Onvalidate trigger of main form field…

Still haven’t been able to filter

I wrote this code on the OnAfterGetRecord trigger of the main form. Is there something missing?

(the form_diario_fusao is a form variable of the subform,)

recDiarioFusao.SETRANGE(DescMaterial, “Cod. Material”);
MESSAGE(’%1’, “Cod. Material”);
MESSAGE(’%1’, recDiarioFusao.DescMaterial);

I think you need to create a function in Subform and call it from main form by passing the field value


Also, just think about where you put your code. OnAFTERGetRecord. As in you have already gotten the data. Would you filter the data after you’ve already gotten it from the database, or before?

I read that somewhere, and it makes sense, but what would that function do?

You need to reference directly to the Subform control and not just another instance of the form.

Give the subform control a name in the properties. Let’s say MySubForm.

Then you can use:


Or via function on the Subform as suggested.

Function should filter the Line records based on the passed value…

Thomas Martin’s solution worked. Thanks!!

if you modify the value in main form type field from abc to xyz?

is it filtering the subform?

Yes and no.

No, because it’s not an editable field.
Yes, because it changes when I change the record (and the value is different).