Results 1 to 13 of 13
  1. #1
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114

    Post Choose a cell based on data in other cells within the same record

    I want to have a query do the following:

    If value A is greater than Col1 but less than Col2 then return cell Col3.

    Here's an example:

    Col1 | Col2 | Col3
    1 | 5 | 100
    6 | 10 | 200
    11 | 20 | 300

    I enter a value (8) into a record. The query runs and compares that value to the records in the table above. It would return 200 because 8 is greater than 6 but less than 10.

    I'm thinking it would be something like an "If, Then" statement in Excel but am not sure how the statement should look in the query.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    select *, iif([a] between [col1] and [col2],[col3],null) as result from table
    or
    select *, iif([a] >= [col1] and [a] <=[col2],[col3],null) as result from table

  3. #3
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    no 'if' required - can do it with the critieria

    Code:
    SELECT Col3 FROM myTable WHERE [Enter an number] between Col1 and Col2

  4. #4
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    Ajax -

    I tried the method you described as it seemed easier. This is what I entered in the Field line:

    Expr4: SELECT [DVRslt] FROM [DVVtmnMnrl] WHERE [Expr1] between [Lwr] and [Uppr]

    However, it keeps telling me the syntax is incorrect and that I should enclose the subquery in parentheses.

  5. #5
    IrogSinta is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Posts
    103
    You don't enter it in a Field line. You need to change your query view from Design View to SQL view and add the Select string there. You then can switch back to Design View to see what it looks like in that view.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Ajax's example is a full SQL statement, not an expression for a field.
    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
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    Um... yeah. I totally knew that.

    So, after correctly adding the statement in SOL I get the error: "Syntax error in FROM clause."

    Here's my SQL. I know it's something super simple but given that I'm just a hack it's beyond me.

    SELECT [1Clrs].AmntClrs
    FROM 1Clrs


    SELECT Col4
    FROM [DVVtmnMnrl]
    WHERE [1Clrs].AmntClrs between Col2 and Col3;


    Should I name the table and columns? Something like:

    SELECT [DVVtmnMnrl].DVRslt
    FROM DVVtmnMnrl
    WHERE [1Clrs].AmntClrs between [DVVtmnMnrl]![Lwr] and [DVVtmnMnrl]![Uppr];
    Last edited by Eddy Sincere; 07-06-2015 at 11:07 PM. Reason: Had an additional thought.

  8. #8
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Not at all clear what you are trying to achieve and your sql does not make sense - perhaps better to explain what tables you have, their field names and any references to forms (form name and control name)

  9. #9
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    Sure.

    I have 2 tables:

    Table 1 - Clrs - It has an amount filed (ClrsAmnt).
    Tbale 2 - DVVtmnMnrl - It has three columns, Upper, Lower and DVRslt

    I want the query to to pull the ClrsAmnt value, compare it to the Upper and Lower values in DVVtmnMnrl table and return the respective DVRslt value. Your solution above seems to be what I need, I just don't know the specific syntax. I attached the DB in it's basic form so you can see what I'm talking about.

    Also, there's an example in my original post.
    Attached Files Attached Files

  10. #10
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    not at all clear what you require but suspect you mean this

    Code:
    SELECT DVVtmnMnrl.DVRslt
    FROM 1Clrs, DVVtmnMnrl
    WHERE ((([1Clrs].AmntClrs) Between [lwr] And [uppr]));

  11. #11
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    For not understanding what I'm asking for you sure nailed it. That's precisely what I'm looking for. Thanks a lot!

    Basically, I want a primary value in one table to be compared against two values in another table. If the primary value fits between the numbers then I want it to return the result in the last column.

    The reason I'm asking is that I have some odd rules about how to display certain values, i.e. 7.5 can be displayed as 10, but not 8. It's weird because normally I could use rounding rules but there are too many "if, then" clauses so it's easier to just compare the primary value to a table with "hard-coded" values. I've attached a copy of the DB with your SQL statement in the query so you can see what I'm talking about.

    Anyway. Thanks for the help, it's sincerely appreciated. Question solved!
    Attached Files Attached Files

  12. #12
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Glad I guessed right

    if you wanted to use this in a more complex query (such as where you have left or right joins) where Cartesians (i.e. no join) are not allowed then although possibly slower for large datasets another way this can be done is

    SELECT IDItm, AmntClrs, (select top 1 DVRslt FROM DVVtmnMnrl WHERE AmntClrs between lwr and uppr) AS DVRslt
    FROM 1Clrs;

    I think you were trying to do the bit in red in post #4

    Also ensure in your live db that lwr and uppr are indexed

  13. #13
    Eddy Sincere is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Location
    Carson City, NV
    Posts
    114
    Nice! I'll definitely keep it in my tool box.

    Thanks again for the help.

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

Similar Threads

  1. Merging Data from 3 Cells to One Cell
    By Upon66 in forum Queries
    Replies: 5
    Last Post: 08-11-2012, 11:04 AM
  2. Replies: 1
    Last Post: 10-23-2011, 10:54 AM
  3. Replies: 7
    Last Post: 09-15-2011, 01:58 PM
  4. Replies: 0
    Last Post: 04-18-2011, 01:01 PM
  5. Replies: 1
    Last Post: 03-02-2009, 10:12 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