Subform Filter from Mainform ComboBox

klopstan

New Member
Joined
Dec 10, 2009
Messages
7
Hi guys, 1st time poster, long time lurker ; )

I know this issue has been covered a number of times, but I've been experiencing problems applying the various solutions listed to my problem.

Basically, I have a form (frmCompany) which lists Company Details and a linked subform (frmContractsSubform) as a datasheet that shows the Company's Contracts that I would like to filter to show grouped records, particularly by the field "ContractYear" which lists the year the Contract became active, 2001, 2002 and so on.

I would like the user to be able to Select a year value from a ComboBox on the main form that will then filter the corresponding records for the linked company subform datasheet.

I have attempted to use the following code:

Private Sub cboFilterYear_AfterUpdate()
Me.frmContractsSubform.Form.Filter = "[ContractYear] = " & Me.cboFilterYear

End Sub

I have also tried including the following code as a second line:

Me.frmContractsSubform.Form.FilterOn = True



But receive the error: Runtime Error '3071' the expression is typed incorrectly.

I figured that once I have the concept down, I can apply it to other fields.

Any advise or assitance would be greatly appreicated, and apologies in advance if this question is somewhat idiotic :)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Your question isn't entirely clear so I am guessing here.

I suspect that [ContractYear] might not be numeric. Try this
Code:
Private Sub cboFilterYear_AfterUpdate()
  Me.frmContractsSubform.Form.Filter = "[ContractYear] = '" & Me.cboFilterYear & "'"
  Me.frmContractsSubform.Form.FilterOn = True
End Sub
The FilterOn=True line should be included.

Also you need to be sure that the first column of the combo is the ContractYear and not some other field which is hidden. Look at the RowSource to check. Then look at Column Widths.
 
Upvote 0
Thanks revver, your suggestion worked and I've been able to apply it to other areas in my db. Much appreciation and kind regards to you :)
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top