Results 1 to 8 of 8
  1. #1
    vismj is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    4

    How to pass checkbox value to qeury criteria

    I have a table named "tblNames" with the following field:

    • ID Autonumber
    • Name Short text
    • Active yes/no


    I have a form called "frmNames" with:

    • A check box called “ActiveOnly”
    • A combo box called “Names”


    The combo box “Names” should list:

    • all records from tblNames when “ActiveOnly” is not checked
    • only records where active=yes when “ActiveOnly” is checked


    How does the query for the combo box looks like? I tried a lot but nothing works.


    Who can help me?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    (you should use Msoft naming conventions to help differentiate objects from fields or values ...combobox = cboNames, checkbox = chkActive

    qry sql=
    select * from tbl where [NAME] = forms!frmNames!cboNames AND [Active] = forms!frmNames!chkActive

  3. #3
    vismj is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    4
    Thanks for your help, but this is not working. Maybe I did not explain my case good enough.

    The criteria for the "active" field in the query that I use as data source for the combo box is depending on the value of the check box. But in stat of the value "no" is need "all".

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by vismj View Post
    Thanks for your help, but this is not working. Maybe I did not explain my case good enough.

    The criteria for the "active" field in the query that I use as data source for the combo box is depending on the value of the check box. But in stat of the value "no" is need "all".
    If you have a form with an unbound check box and you want to change the data in the combo box each time the check box value changes, I think the best way would be to use some code in the After Update event of the check box that would reassign the Row Source property of the combo box .
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    vismj is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    4
    I did some homework, but nothing works. So I tried all over again.

    This is what I’ve got so far:

    I have a table with a Text field “chrCompany” and a Boolean field “ysnCurrent”.
    I have a form with a checkbox “chkCurrentOnly” and a combo box “cboSelectCompany”.

    What I want is a list of companies in the combo box. Depending on the checkbox the list must show all companies or only the current companies.

    In the event properties of the checkbox at after-update I call the procedure “SetCompanyCurrentFilter(chkCurrentOnly)”.

    In the query I call the procedure “GetCompanyCurrentFilter()” as a criteria of the “Current” field.

    When I display the “GetCompanyCurrentFilter()” value as a text in my form it shows the right criteria string, so that is working.

    Why doesn’t the query work? What am I doing wrong?

    Public varCompanyCurrentFilter As String

    Function GetCompanyCurrentFilter() As String
    GetCompanyCurrentFilter = varCompanyCurrentFilter
    End Function

    Function SetCompanyCurrentFilter(ValueIn As Boolean)
    Dim ValueOut As String
    If ValueIn Then
    ValueOut = “Yes”
    Else
    ValueOut = “‘Yes’ or ‘No’”
    End If
    varCompanyCurrentFilter = ValueOut
    DoCmd.Requery (cboSelectCompany)
    End Function

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    If you are getting help in other forums, you should let others now and post links to any other threads you started.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    What I want is a list of companies in the combo box. Depending on the checkbox the list must show all companies or only the current companies.
    So it seems the rowsource of the combobox should be (depending on field and table names) similar to

    Code:
    Select  chrCompany from tblCompany where ysnCurrent = True
    You can test the sql in the query designer.

    I think you may have some structure issues, but I really would recommend you tell us the business facts in plain English. Readers need to know what you are trying to do before they can offer suggestions/options as to how this might be accomplished.

    Good luck.

  8. #8
    vismj is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Mar 2015
    Posts
    4
    I find the solution!

    I rewrote the source code to:

    Public varCompanyCurrentFilter As Boolean

    Function GetCompanyCurrentFilter() As Boolean
    GetCompanyCurrentFilter = varCompanyCurrentFilter
    End Function

    Function SetCompanyCurrentFilter(ValueIn As Boolean)
    varCompanyCurrentFilter = ValueIn
    DoCmd.Requery (cboSelectCompany)
    End Function

    Then I use the following code in the criteria of the Current field of the query that I use for the combo box:

    Like IIf(GetCompanyCurrentFilter();True;"*")

    This is returning the current companies if the checkbox is checked and all companies when the checkbox is unchecked.

    Last edited by vismj; 04-18-2015 at 10:26 AM. Reason: simplify the source code

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Criteria to show checkbox value
    By keithb in forum Queries
    Replies: 5
    Last Post: 01-31-2015, 02:14 PM
  2. Replies: 2
    Last Post: 09-19-2014, 07:16 AM
  3. cant understand an expression in qeury
    By raffi in forum Macros
    Replies: 2
    Last Post: 08-31-2014, 09:42 AM
  4. Replies: 1
    Last Post: 06-24-2013, 07:50 AM
  5. using checkbox as iif criteria
    By TheShabz in forum Queries
    Replies: 3
    Last Post: 01-18-2011, 05:10 PM

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