Create reports based on multiple combo box selections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jvan2008
    New Member
    • Mar 2008
    • 6

    Create reports based on multiple combo box selections

    "Form1"
    combobox "cboModel"
    Row Source
    Code:
    SELECT [tblModel].[ID], [tblModel].[Model] FROM tblModel ORDER BY [Model];
    combobox "cboContactName "
    Code:
    SELECT [Query1].[Contact Name] FROM Query1 ORDER BY [Contact Name];
    quick explanation: I have a table named tblCOntacts and in this table i have 3 fields "LastName" and "FirstName" and "Initial" becuase i have multiple employess that have the same last names and sometimes same first and last name. So I have a query titled "Query1" with 2 feilds in it titled:
    "File AS" and "Contact Name". Each of these fields combines the first and last name into one field. one does it first name last name and the other last name first name.

    and then on form1 i have two cmdbuttons "cmdApplyFilter " and "cmdRemoveFilte r"
    I have tried two differant codes
    Here is the 1st code:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub cmdApplyFilter_Click()
        Dim strModel As String
        Dim strContactName As String
        Dim strFilter As String
    ' Check that the report is open
      DoCmd.OpenReport "rptContacts", acPreview, , strFilter
    ' Build criteria string for Office field
        If IsNull(Me.cboModel.Value) Then
            strModel = "Like '*'"
        Else
            strModel = "='" & Me.cboModel.Value & "'"
        End If
    ' Build criteria string for Department field
        If IsNull(Me.cboContactName.Value) Then
            strContactName = "Like '*'"
        Else
            strContactName = "='" & Me.cboContactName.Value & "'"
        End If
    ' Combine criteria strings into a WHERE clause for the filter
        strFilter = "[Model] " & strModel & " AND [ContactName] " & strContactName
    ' Apply the filter and switch it on
        With Reports![rptContacts]
            .Filter = strFilter
            .FilterOn = True
        End With
    End Sub
    
    Private Sub cmdRemoveFilter_Click()
        On Error Resume Next
    ' Switch the filter off
        Reports![rptContacts].FilterOn = False
    End Sub
    Using this code i get a.) two small form popups asking for the model value and contactname value then it returns a blank report

    Here is the seconf code I tried:
    Code:
    Private Sub cmdOpenReport_Click()
        Dim strModel As String
        Dim strContactName As String
        Dim strFilter As String
        strFilter = "1=1 "
    ' Build criteria string for Office field
        If Not IsNull(Me.cboModel) Then
            strFilter  = " AND [Model] ='" & Me.cboModel.Value & "'"
        End If
    ' Build criteria string for Department field
        If Not IsNull(Me.cboContactName) Then
            strFilter = " AND [Contact Name] ='" & Me.cboContactName.Value & "'"
        End If
    ' Apply the filter and switch it on
      DoCmd.OpenReport "rptContacts", acPreview, , strFilter
    
    End Sub
    For this i get the following error:
    Run Time Error '3075':
    Syntax error (missing operator) in query expression ' AND [Model]='*620".

    Any help please!!
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32636

    #2
    A question would be a good starting point. What do you want to know?

    Comment

    • jvan2008
      New Member
      • Mar 2008
      • 6

      #3
      Well lets see the title is "Create reports based on multiple combo box selections". Im going to say, how do I do it? or maybe, why isnt my code working? I thought it was prety obivous

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32636

        #4
        Originally posted by jvan2008
        I thought it was prety obivous
        I doubt you'll want help from someone who thought it was a rambling mess then.

        Good luck.

        Comment

        • jvan2008
          New Member
          • Mar 2008
          • 6

          #5
          Well thanks for all your help sport

          Comment

          Working...