troubleshooting Question

Filter Form records shown using varCriteria

Avatar of Ludique
LudiqueFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft AccessVisual Basic Classic
16 Comments1 Solution585 ViewsLast Modified:
I have an unbound form with 9 fields - 1 xext, 1xYes/No, 7xCombo boxes.  After extensive trawling though EE I decided to use the solution explained in by Harfang which I have amended (as far as I'm able) with my own specifics.  

When the user has entered/selected data (or not) and clicks the OK button I would like it to open the form frmPolicy which is based on the query forFrmPolicy.

The button to Clear the form before entering data works fine.  
However, on clicking OK I just get a message like:   ContactID=7 AND ProviderID=30 AND PolicyPrint=-1
Presumably indicating that it's got the selected data form the form but not doing anything with it.  Right?  

The solution feels so close I can almost smell it!  So what have I missed?
Here's my version of the code:

Option Compare Database

Option Explicit

Private Sub cmdClear_Click()
   ContactID = Null
   ProviderID = Null
   ProductID = Null
   PolicyStatusID = Null
   Kfdreference = Null
   PolicyFundFormatID = Null
   AdviserID = Null
   ParaplannerID = Null
   PolicyPrint = Yes
End Sub

Private Sub cmdQuery_Click()

    Dim varCriteria As Variant

    varCriteria = Null
        If Not IsNull(ContactID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("ContactID", dbLong, ContactID)
    If Not IsNull(ProviderID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("ProviderID", dbLong, ProviderID)
    If Not IsNull(ProductID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("ProductID", dbLong, ProductID)
    If Not IsNull(PolicyStatusID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("PolicyStatusID", dbLong, PolicyStatusID)
    If Not IsNull(Kfdreference) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("Kfdreference", dbText, Kfdreference)
    If Not IsNull(PolicyFundFormatID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("PolicyFundFormatID", dbLong, PolicyFundFormatID)
    If Not IsNull(AdviserID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("AdviserID", dbLong, AdviserID)
    If Not IsNull(ParaplannerID) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("ParaplannerID", dbLong, ParaplannerID)
    If Not IsNull(PolicyPrint) Then _
        varCriteria = varCriteria + " AND " & BuildCriteria("PolicyPrint", dbBoolean, PolicyPrint)

    MsgBox Nz(varCriteria, "No Criteria selected - All Records will be shown")

    'Exit Sub
    ' store into a query definition for later use:
    CurrentDb.QueryDefs("qselTemp").SQL = "SELECT * FROM forFrmPolicy" & " WHERE " + varCriteria & ";"
    ' open a report:
    ' DoCmd.OpenReport "rptPolicies", acViewPreview, WhereCondition:=Nz(varCriteria)

    ' filter an open form:
    Forms!frmPolicy.Filter = Nz(varCriteria)
    Forms!frmPolicy.FilterOn = Not IsNull(varCriteria)

End Sub


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros