I made the code to filter a subform from a main form. In the main form i have multiple textboxes and comboboxes that filter the subform. In the code they are connected, so they filter already filtered subform if i have used one or more of textbox or comboboxes already.
It works alright when I use comboxes or when I paste the text in the textboxes, but it doesnt filter when while i type (On Change) in textboxes or comboboxes.
The code for filtering is in a different sub so i can easy put it in on textboxes and comboboxes.
Does anyone have a solution for this so that it works on change also (when I start typing that it filters) because it doesn't do that, only selecting in a combobox or pasting into textbox.
I have tried adding .Text to comboboxes and textboxes but it returns a error "run time error 2185, "You can't reference a property or method for a control unless the control has the focus ..."."
Adding .Value also didnt made any change, but it was without the error.
EDIT: Also it starts filtering, but strange when I use backspace to delete what I typed.
It works alright when I use comboxes or when I paste the text in the textboxes, but it doesnt filter when while i type (On Change) in textboxes or comboboxes.
The code for filtering is in a different sub so i can easy put it in on textboxes and comboboxes.
Does anyone have a solution for this so that it works on change also (when I start typing that it filters) because it doesn't do that, only selecting in a combobox or pasting into textbox.
I have tried adding .Text to comboboxes and textboxes but it returns a error "run time error 2185, "You can't reference a property or method for a control unless the control has the focus ..."."
Adding .Value also didnt made any change, but it was without the error.
EDIT: Also it starts filtering, but strange when I use backspace to delete what I typed.
Code:
Private Sub txtVlaV_Change()
FilterCode
End Sub
Private Sub FilterCode()
Dim strWhere As String
Dim lngLen As Long
Const conJetDate = "\#dd\/mm\/yyyy\#"
If Not IsNull(Me.cboKatV) Then
strWhere = strWhere & "([KatV] Like '*" & Me.cboKatV & "*') AND "
End If
If Not IsNull(Me.cboMarkaV) Then
strWhere = strWhere & "([MarkaV] Like '*" & Me.cboMarkaV & "*') AND "
End If
If Not IsNull(Me.txtModelV) Then
strWhere = strWhere & "([ModelV] Like '*" & Me.txtModelV & "*') AND "
End If
If Not IsNull(Me.txtTipM) Then
strWhere = strWhere & "([TipMotora] Like '*" & Me.txtTipM & "*') AND "
End If
If Not IsNull(Me.txtRig) Then
strWhere = strWhere & "([Rig] Like '*" & Me.txtRig & "*') AND "
End If
If Not IsNull(Me.txtBrSaj) Then
strWhere = strWhere & "([BrSaj] Like '*" & Me.txtBrSaj & "*') AND "
End If
If Not IsNull(Me.cboVrGor) Then
strWhere = strWhere & "([VrGor] Like '*" & Me.cboVrGor & "*') AND "
End If
If Not IsNull(Me.cboVrstPog) Then
strWhere = strWhere & "([VrstPog] Like '*" & Me.cboVrstPog & "*') AND "
End If
If Not IsNull(Me.txtVlaV) Then
strWhere = strWhere & "([VlaV] Like '*" & Me.txtVlaV & "*') AND "
End If
If Not IsNull(Me.txtKorV) Then
strWhere = strWhere & "([KorV] Like '*" & Me.txtKorV & "*') AND "
End If
If Not IsNull(Me.txtOdDat) Then
strWhere = strWhere & "([DatV] >= " & Format(Me.txtOdDat, conJetDate) & ") AND "
End If
If Not IsNull(Me.txtDoDat) Then
strWhere = strWhere & "([DatV] >= " & Format(Me.txtDoDat, conJetDate) & ") AND "
End If
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
Exit Sub
Else
strWhere = Left$(strWhere, lngLen)
Me.subPretV.Form.Filter = strWhere
Me.subPretV.Form.FilterOn = True
Me.subPretV.Form.Requery
End If
End Sub
Last edited: