MonkeyChico
Registered User.
- Local time
- Today, 12:11
- Joined
- May 11, 2010
- Messages
- 18
I have some great VBA code that I used in the past to filter a form. Now I'm trying to apply this code to filter a subform but I have no idea how to modify it (I know pretty much nothing about VBA, I just relpicate what I can find on here and Google). My unbound filter fields/combo boxes are on the main form.
Here's what I have:
- Main form uses tbl_Quote Book List
- Subform uses tbl_Quoting Data
Here's the code:
Private Sub cmdFilter_Click()
Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
If Not IsNull(Me.qcustomername) Then
strWhere = strWhere & "([Customer name] Like ""*" & Me.qcustomername & "*"") AND "
End If
If Not IsNull(Me.qquotemonth) Then
strWhere = strWhere & "([Quote Month] = """ & Me.qquotemonth & """) AND "
End If
If Not IsNull(Me.qLOB) Then
strWhere = strWhere & "([Line of Business] = """ & Me.qLOB & """) AND "
End If
If Not IsNull(Me.qquotestatus) Then
strWhere = strWhere & "([Quote Status] = """ & Me.qquotestatus & """) AND "
End If
If Me.QQaflag = -1 Then
strWhere = strWhere & "([QA Flag] = True) AND "
ElseIf Me.QQaflag = 0 Then
strWhere = strWhere & "([QA Flag] = False) AND "
End If
'See if the string has more than 5 characters (a trailng " AND ") to remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub
Here's what I have:
- Main form uses tbl_Quote Book List
- Subform uses tbl_Quoting Data
Here's the code:
Private Sub cmdFilter_Click()
Dim strWhere As String 'The criteria string.
Dim lngLen As Long 'Length of the criteria string to append to.
Const conJetDate = "\#mm\/dd\/yyyy\#" 'The format expected for dates in a JET query string.
If Not IsNull(Me.qcustomername) Then
strWhere = strWhere & "([Customer name] Like ""*" & Me.qcustomername & "*"") AND "
End If
If Not IsNull(Me.qquotemonth) Then
strWhere = strWhere & "([Quote Month] = """ & Me.qquotemonth & """) AND "
End If
If Not IsNull(Me.qLOB) Then
strWhere = strWhere & "([Line of Business] = """ & Me.qLOB & """) AND "
End If
If Not IsNull(Me.qquotestatus) Then
strWhere = strWhere & "([Quote Status] = """ & Me.qquotestatus & """) AND "
End If
If Me.QQaflag = -1 Then
strWhere = strWhere & "([QA Flag] = True) AND "
ElseIf Me.QQaflag = 0 Then
strWhere = strWhere & "([QA Flag] = False) AND "
End If
'See if the string has more than 5 characters (a trailng " AND ") to remove.
lngLen = Len(strWhere) - 5
If lngLen <= 0 Then 'Nah: there was nothing in the string.
MsgBox "No criteria", vbInformation, "Nothing to do."
Else 'Yep: there is something there, so remove the " AND " at the end.
strWhere = Left$(strWhere, lngLen)
'Finally, apply the string as the form's Filter.
Me.Filter = strWhere
Me.FilterOn = True
End If
End Sub