Search code examples
vbafiltersubform

VBA - Filter subform on unbound field in form


I am passing a value from a lookup form to another form. The value shows in an unbound field on the form (works great). I also want the subform to be filtered by what is in this unbound field. So, for example, I pick 2016 on my lookup form. It brings up another form, enters the value 2016 into an unbound field called yearvar. I cannot get it to filter what's in the subform. Here's my code that's not working:

yearvar.Value = Me.OpenArgs 'Enters year from lookup form into field call yearvar
Forms!frmEthEntCatYr.frmEthEntCatSubCatYr.Form.Filter = "[EthYear]= " & Chr(34) & Forms!frmEthEntCatYr.yearvar & Chr(34)

Form name is frmEthEntCatYr. Subform name is frmEthEntCatSubCatYr. Unbound field on form is called Yearvar. Field on subform that I want to filter is called EthYear.

What am I doing wrong? Thanks in advance!


Solution

  • Problem solved and it was an easy fix (thanks to someone helping me with my dah moment). I had originally tried to link the main form and subform using the Subform field linker wizard, but the unbound field was not showing (yearvar) in the Master Fields dropdowns in the Link wizard. Someone suggested just putting it in the Property Sheet - in Link Master fields, I typed yearvar and in Link Child Fields, I typed Ethyear. Works perfectly now. Lesson of the day for me: sometimes the wizards do not do what we want them to do, and you have to do it the manual way. And I didn't even have to write code to do this!