Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    30

    Using field reference prevents using criteria

    When designing a query, having a column use a reference to another field works fine until I try and put a criterion on it at which point running the query brings up the "enter parameter value" box.

    So this works:
    Field fieldname
    Table tablename
    Criteria >5

    And this works:
    Field User Field Name: [tablename.fieldname]
    Table
    Criteria >5

    And this works:
    Field User Field Name: [tablename.fieldname] Other User Field Name:[User Field Name] + 1
    Table
    Criteria

    But this doesn't:


    Field User Field Name: [tablename.fieldname] Other User Field Name:[User Field Name] + 1
    Table
    Criteria >5

    The actual query I'm making is more complex, with both fields involving formulas and I'd rather not repeat the formula in the second field - I want to just refer to it. I don't understand why this wouldn't work. Doesn't the second field simply represent a value that can have a criterion?

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    since you didn't post the actual names, we can only guess that you've misspelled one of them - the table or field name you're being prompted for.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    fhickler,
    We only know what you tell us and you have told us nothing about the application/database involved. Your issue is specific to use of or mechanics of the Access query grid (or so it seems).
    If you overview your issue in plain English, you may find it more efficient to getting a focused response.
    I have a database dealing with X people, Y orders and Z products. I want to find out How many Y(Orders) have more than 4 Products.

    Good luck with your project.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    just guessing perhaps you are grouping the data

  5. #5
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    30
    Since nobody seems to understand what I'm saying, I made a simple example and am posting screenshots:

    Click image for larger version. 

Name:	page1.png 
Views:	16 
Size:	116.6 KB 
ID:	37609

    Click image for larger version. 

Name:	page2.png 
Views:	17 
Size:	107.3 KB 
ID:	37610

    The question is: why doesn't the second criterion work?

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I'd say it's because you're using an alias rather than the field name. The calculations shouldn't involve alias names but rather the field names. On my phone so can't verify right now.

  7. #7
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    30
    Quote Originally Posted by Micron View Post
    The calculations shouldn't involve alias name
    As you can see from the example, the calculation works fine when using an alias name. It's only when I add a criterion that it doesn't work.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    One thing I never do is use spaces in object names.... just say'n.

    Have you tried switching to SQL view and compare the SQL for the queries?
    What does the SQL look like for for the queries?

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,924
    You got it.

    Calculation referencing another calculated field in same query cannot have filter criteria. Would have to repeat the first calc.
    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.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Quote Originally Posted by fhickler View Post
    As you can see from the example, the calculation works fine when using an alias name. It's only when I add a criterion that it doesn't work.
    BUT you are trying to compare/filter against an unknown. It's like being one level removed. Eliminate the alias and it will likely work

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,924
    Tried with and without alias. Does not work. Access just throws in an alias Expr1 even when the Show box is not checked.
    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.

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Then the "unknown" isn't the alias - it's the first calculation? That would make sense.

  13. #13
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    why doesn't the second criterion work?
    rather than looking at the querygrid, look at the sql it generates. Criteria work on the original source not alias. Your Alias is a calculation. Create a simple query


    Code:
    SELECT fld1*2 as Num
    FROM myTable
    WHERE fld1*2>10
    Note that the alias is not used in the criteria

    The workaround is to have two queries the second to use the first

    Code:
    SELECT A.*, Num*2 as Num2
    FROM (SELECT *, fld1*2 as Num
    FROM myTable) AS A INNER JOIN myTable ON A.ID=myTable.ID
    WHERE Num*4>20

  14. #14
    fhickler is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Nov 2018
    Posts
    30
    Ok. Thanks all. I obviously have some things to learn about Access, especially SQL. I should train myself to think of the query designer as just a way to generate SQL code rather than an end in itself. It just seemed odd, since the alias resolved to a value, so why not set a criterion on it? A similar thing in excel would work. But, of course, Access != Excel.

    The reason I wanted to use the alias name is that in the actual database, is that the formula is more complex, and I wanted to refer to it many times in other columns - I didn't want to have to repeat the formula. What if the formula changed, for example? Maybe a VBA function is in order (another area I need to improve on). Or I can just export the query to excel and do the calculation there.

  15. #15
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,428
    The reason I wanted to use the alias name is that in the actual database, is that the formula is more complex, and I wanted to refer to it many times in other columns - I didn't want to have to repeat the formula. What if the formula changed, for example?
    or do as I suggested in post #13 - a technique I often use for these situations. VBA will probably be slower and exporting to Excel will probably be slower as part of an overall process (time required to export, open excel etc).

    Access != Excel
    agreed, but it is more like

    Excel != database (of any sort)

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

Similar Threads

  1. Calculated field prevents new record
    By Chrtalgo in forum Programming
    Replies: 5
    Last Post: 01-24-2018, 04:52 PM
  2. Replies: 3
    Last Post: 06-26-2014, 03:02 PM
  3. Replies: 3
    Last Post: 07-20-2012, 11:41 AM
  4. Query Prevents formatting of field
    By Chelcone in forum Queries
    Replies: 3
    Last Post: 04-20-2012, 11:43 AM
  5. Replies: 1
    Last Post: 05-18-2011, 12:23 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