Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17

    Unhappy Query from 2 combo boxes

    Hello I am trying to build a query that gets criteria from two combo boxes (cmb1 and cmb1) in a form (frm1) to build and populate a report.
    In detail I need to set the query to work in the event that:
    • the user makes a selection from only one of the combos
    • the user makes selections from both combos.


    It actually works if I check by running the query, but if I run the form that contains the two combo boxes, the self-generated report is filled with ALL the records from the table, unfiltered!

    This is what I put in the query (omitted insignificant details):


    Code:
    Field1: IIf([Forms]![frm1]![cbo1]<>"*";[tbl1.val1]=[Forms]![frm1]![cbo1];True)
    Field2: IIf([Forms]![frm1]![cbo2]<>"*";[tbl1.val2]=[Forms]![frm1]![cbo2];True)
    Criteria (both): <>False
    What am I missing?
    Last edited by frons; 05-13-2018 at 12:07 PM. Reason: Update

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Try:

    WHERE fieldname1 LIKE [Forms]![frm1]![cbo1] & "*" AND fieldname2 LIKE [Forms]![frm1]![cbo2] & "*"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    Still populating the forum with all the records if I run it through the form (although the query itself perfectly works).
    I'm sure it has something to do with leaving one of the two comboboxes unchecked (i.e. if I would like to select only one of the two options), but don't know how and why

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Your OP says you want to populate a report. So is this query with filter criteria referencing comboboxes the report RecordSource?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    Correct, the report has the query as record source.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    You said the query works. So the report is properly filtered?

    If you want form to also be filtered, try code to requery the form. Or click on the Refresh button on the ribbon.

    I don't use dynamic parameterized queries. I prefer code to build filter criteria. Review http://allenbrowne.com/ser-62.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    Quote Originally Posted by June7 View Post
    You said the query works. So the report is properly filtered?
    No, that's exactly the issue: if I run the query from its ribbon, it perfectly works, but if I run the form, the report it opens it's unfiltered at all.
    Quote Originally Posted by June7 View Post
    If you want form to also be filtered, try code to requery the form. Or click on the Refresh button on the ribbon.
    I think you meant Report, not Form. I'm ultimately interested in the Report to be properly filtered when opened from the Form.
    I also tried to run the Report by opening it by itself and not from the Form: it (of course) asks me for the parameters and once I gave them it perfectly works, but not if I open it through the Form

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    No, I meant what I said. You stated in post 3 you 'run it through the form' which indicated to me you wanted to apply filter to form as well as report.

    By 'from its ribbon' do you mean open query object from Navigation Pane?

    Not sure what you mean by 'open it through the form'. If the query is the report RecordSource then report should be filtered same as opening the query object. Wouldn't matter if the report is opened manually from Navigation Pane (as long as form is open) or by code behind a button on form.

    At this point, you will have to provide the database for review if still need help.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    Quote Originally Posted by June7 View Post
    By 'from its ribbon' do you mean open query object from Navigation Pane?
    Yes

    Quote Originally Posted by June7 View Post
    Not sure what you mean by 'open it through the form'.
    The Report is opened through the Form via a onClick event on a send button below the two comboboxes.

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    You seem to be making this more complicated than it needs to be
    There are several ways of doing this

    See attached for a quick example based on a list of students for a fictitious school
    As in your case, the report is based on a query which can be filtered by one or both combo boxes.
    Note the use of wildcards in the filters
    The report includes a label indicating which filters were used if any

    The query used for the report also allows unfiltered output where neither combo is completed
    If you don't want that, the easiest way is to have the button disabled until one or other combo is updated

    There are other ways of doing the same thing including Me.FilterOn=True/False, Me.Filter = ....
    In a real world example, I would use VBA rather than a query but the principle is the same.
    I would also add buttons to clear the combo selection

    However I'll leave you to adapt for your own purposes

    HTH
    Attached Files Attached Files
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    Thank you Colin, now it works in every case (one selection, both selections, no selection at all) and the Report is correctly populated.

    Only issue is I would like to have the Header stating what kind of filter is on at the moment; I did like you suggested with a label, but it never shows anything.
    The two parameter that can be filtered are both Short Text type.

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    I thought you'd want that feature...
    Make sure you
    1. Assign a string variable to each item and define both as public variables in a module
    2. Set the values in the combo after update event code
    3. Use the report header format event for the caption
    4. Set out the caption as I did for strSurname for both fields e.g.
    Code:
    "Surname Like " & strSurname & "; Forename Like " & strForename & ""
    or if you want to enclose in single quotes use
    Code:
    "Surname Like '" & strSurname & "'" & "; Forename Like '" & strForename & "'"
    Last edited by isladogs; 05-14-2018 at 07:54 AM. Reason: Added further example, corrected typo
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  13. #13
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    I have followed all the steps, adapting to my datas and needs, but nothing is shown.
    This is the Sub ReportHeader_Format:
    Code:
    Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)    
                If Nz(strTema, "") <> "" And Nz(strTurno, "") <> "" Then
                  Me.lblInfo.Caption = "Tema " & strTema & "; Turno " & strTurno
               ElseIf Nz(strTema, "") <> "" Then
                 Me.lblInfo.Caption = "Theme " & strTema & ""
              ElseIf Nz(strTurno, "") <> "" Then
                 Me.lblInfo.Caption = "Turn " & strTurno & ""
              Else
                 Me.lblInfo.Caption = "Everybody"
             End If
            End Sub
    I can't understand the mistake

  14. #14
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,980
    You didn't modify the first option
    Code:
    Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)    
                If Nz(strTema, "") <> "" And Nz(strTurno, "") <> "" Then
                  Me.lblInfo.Caption = "Tema " & strTema & "; Turno " & strTurno & ""
               ElseIf Nz(strTema, "") <> "" Then
                 Me.lblInfo.Caption = "Theme " & strTema & ""
              ElseIf Nz(strTurno, "") <> "" Then
                 Me.lblInfo.Caption = "Turn " & strTurno & ""
              Else
                 Me.lblInfo.Caption = "Everybody"
             End If
            End Sub
    If you are saying none of these work, then you must have forgotten something else as well e.g.
    1. Have you added a label called lblInfo to the report header and made it visible
    2. Did you set the values of strTema and strTurno in the combo after update events
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  15. #15
    frons's Avatar
    frons is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Apr 2015
    Location
    Belgium
    Posts
    17
    Quote Originally Posted by ridders52 View Post
    You didn't modify the first option
    Code:
    Private Sub ReportHeader_Format(Cancel As Integer, FormatCount As Integer)    
                If Nz(strTema, "") <> "" And Nz(strTurno, "") <> "" Then
                  Me.lblInfo.Caption = "Tema " & strTema & "; Turno " & strTurno & ""
    I missed the part in red, but nothing changed in the end

    Quote Originally Posted by ridders52 View Post
    If you are saying none of these work, then you must have forgotten something else as well e.g.
    1. Have you added a label called lblInfo to the report header and made it visible
    Yes
    Quote Originally Posted by ridders52 View Post
    2. Did you set the values of strTema and strTurno in the combo after update events
    Yes
    Code:
    Private Sub cboTurno_AfterUpdate()    
      strTurno = Nz(Me.cmbTurno, "")
    End Sub
    
    Private Sub cboTema_AfterUpdate()
      strTema = Nz(Me.cmbTema, "")
    End Sub
    
    Private Sub btnInvia_Click()
       DoCmd.OpenReport "Rpt Studenti", acViewPreview
    End Sub

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 09-22-2016, 08:42 AM
  2. Replies: 11
    Last Post: 08-06-2014, 09:47 AM
  3. Replies: 9
    Last Post: 06-04-2014, 10:45 AM
  4. Combo Boxes Query Criteria help
    By noaccessguru in forum Queries
    Replies: 2
    Last Post: 04-30-2012, 08:09 PM
  5. Running a query based on 2 combo boxes.
    By blessoni in forum Queries
    Replies: 4
    Last Post: 12-12-2010, 02:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums