Results 1 to 6 of 6
  1. #1
    ali_sh is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    21

    How to use if in query critera

    Hello
    I believe it's simple question but I'm inexperienced in Access so I'll be grateful if you help me on this.
    I created a table:
    Click image for larger version. 

Name:	Capture.PNG 
Views:	14 
Size:	4.3 KB 
ID:	40113

    and a simple query to find what I want:
    Click image for larger version. 

Name:	Capture1.PNG 
Views:	14 
Size:	18.6 KB 
ID:	40114

    but it doesn't work when I enter "b" or anything but "a" ([Table1]![Field1]>2 part doesn't work.)
    How can I write a code for this?


    thanks in advance

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I want to understand better what you are trying to do and why, but I fear that you may have oversimplified things a bit, making it hard to see.
    I have a feeling that the answer to your question is to do it another way, but without having that full understanding, I am not sure what you recommend.

    Can you provide a more realistic example, and explain exactly what it is that you are trying to do?

  3. #3
    ali_sh is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2019
    Posts
    21
    Quote Originally Posted by JoeM View Post
    I want to understand better what you are trying to do and why, but I fear that you may have oversimplified things a bit, making it hard to see.
    I have a feeling that the answer to your question is to do it another way, but without having that full understanding, I am not sure what you recommend.

    Can you provide a more realistic example, and explain exactly what it is that you are trying to do?
    Thank you for your answer
    I have a search form containing a combox with all/yes/no option.
    If user select yes, zero value of a column of table and if select no, other value of that column, and if select "all", all of them with search query should return.
    My actual problem is very similar to what I described before and I,m very curious to understand what's wrong with my written code, so if you tell me which part of the code is wrong I would be able to sovle my problam

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I have never seen it attempted to be done that way. What I have typically done in the past is to create a Form where the user can select query criteria "on-the-fly", and then have VBA to build the SQL code for the query behind the scenes, apply it to my query, and then open the query.

    Kind of like what I did here: https://www.accessforums.net/showthr...587#post435587

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,788
    remove the greater than 2 part (>2) and see what you get. It doesn't make sense.
    EDIT - the approach sort of doesn't either. Why not just have the criteria refer to the combo and make use of what's been selected? There are pitfalls to referring to form controls (e.g. the form has to be open, query will still run if no data is in the control, etc.) but it's not a bad way to learn. Sql in vba should be the end goal of your learning curve on this part of the project but it can be challenging if you have little experience with it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,405
    [Table1]![Field1]>2 is an expression that will return either TRUE or FALSE. Probably not what you want.

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

Similar Threads

  1. query critera not accepting -- (two dashes)
    By tagteam in forum Access
    Replies: 3
    Last Post: 08-17-2016, 03:42 PM
  2. Query critera from form that is now a suborm
    By gem1204 in forum Queries
    Replies: 1
    Last Post: 04-12-2016, 12:12 PM
  3. DSum with Multiple Critera in a Query
    By flamingphoenix in forum Queries
    Replies: 1
    Last Post: 11-11-2014, 11:04 AM
  4. Query Critera w/ two Paths
    By Mike4172 in forum Queries
    Replies: 6
    Last Post: 04-10-2013, 08:47 PM
  5. Query that would search multiple critera and delete
    By justinwright in forum Queries
    Replies: 2
    Last Post: 08-25-2010, 10:05 AM

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