Question : Multiple combobox filtering subform in datasheet view

I have been wrestling with this one for a few days now.

I have a main form with a data source of a tbl containing account info...account number, manager name, etc.  On this form I have a subform in datasheet view, which displays all of the assets assigned to the account.  There will eventually be 200 or so records in the assets tbl, and I wanted to have the information quickly sortable via comboboxes on the main form above the subforms respective fields.  I am only displaying four fields that I would like to filter by on this main form.  They are (Asset_Type, Network, Room and Primary_User).

The goal here is to have on main form load all records displayed in the subform.  When one of the comboboxes is updated, I would like it to filter the records in the subform to match.  I am having issues getting just that to work at this point.

To add to the complexity, I would like the remaining comboboxes to add continuing filters as they are updated.

Example: When user updates cmbType = Laptop, only laptops would be displayed, then if cmbPrimary_User is update to John Doe, I would like to see only the laptops that belong to John Doe and so on.

I have been searching on here for a few days and trying various different coding examples.  I am not very versed with SQL strings, and have not worked with access for a few years now.  My VBA is rough at best and I know that EE is the best place to turn for good solid advice and sample code.

Thanks in advance.

If you would like to see what I have so far I would be more than happy to send supporting info.

Answer : Multiple combobox filtering subform in datasheet view

The simplest way I know is to build a Function that can build your Filter string, and then call that function anytime one of your search criteria controls is updated. For example:

Function BuildSearch()

Dim sFilter As STring

If Nz(Me.cboDeviceType, "") <> "" Then
  sFilter = "DeviceType=" & Me.cboDeviceType
End If

If Nz(Me.cboUserName, "") <> "" Then
  If Len(sFilter) > 0 Then sFilter = sFilter & " AND "
  sFilter = sFilter & " UserName='" & Me.cboUserName & "'"
End If

Me.YourSubformControl.Form.filter = sFilter
Me.YourSubformControl.Form.FilterOn = True

End Function

Now just call that function from your controls:

Sub cboDeviceType_AfterUpdate()
  BuildSearch
End Sub

A few tips:

Filter strings are nothing more than valid SQL WHERE clauses without the word "WHERE", so you must follow the same rules:

Enclose text values in single or double quotes: "UserName='" & Me.cboUserName & "'" is surrounded by single quotes

Enclose Date values in Hash marks: "DateLoaned=#" & Me.txDateLoaned & "#"

Make SURE that you properly refer to your Subform Control. THis is the CONTROL on your main form that hosts the form you're using as a subform. It may or may not be named the same as the form you're using as a subform (i.e. the "Source Object"), so be careful when referring to that control.
Random Solutions  
 
programming4us programming4us