0
votes

This should be simple! I have a large table with 3 numeric fields. I want to calculate a value based on these 3 fields. Then only return records where that value is <1 (say). Trouble is the calculation is too complex for access if I use just one expression. To make it easier to explain let's assume the calc I want is:

fieldx^2 + fieldy^2 + fieldz^2.

So I created a subquery that splits the calculation into 3 fields. One is expr1: fieldx^2, one is expr2:fieldy^2 and expr3 is fieldz^2. Then I create expr4 which is expr1 + expr2 + expr3. Then I use this subquery in another query, which works fine and I can see the field expr4 has been calculated correctly. So I now try to only select records where expr4 <1 using

select * from subquery where expr4 <1

And I get the error "Datatype mismatch in criteria expression". Strangely when I try selecting on expr1,2 or 3 it works fine.

1
You may need to CAST expr4 to a numeric type, eg integer, decimal, float etc. So you would have expr4: CInt(expr1 + expr2 + expr3)twoleggedhorse
I tried using cdbl but it doesn't seem to make any difference.John Fowler
Can you please show the actual calculations for expr1->3.twoleggedhorse
Each is a variation on (([fieldx]-[param1])*0.266)^2. I tried running the query against a subset of records. It worked fine for records after 04/05/2005. 3122 records returned by the subquery. 201 returned by the main query with expr4 <1. But then it gets weird. I changed the date to be after 03/05/2005. 3122 records returned but I get the error again with the query. On 04/05/2005 there is a record but fieldx has no value and I'm only selecting records where fieldx, y and z are >0. So this must be causing the problem because I changed fieldx for this record to be a value and it worked fine.John Fowler
So what seems to be happening is that the query is trying to evaluate the expression expr3 for records before selecting them. But then only giving an error when it has to try and compare against a value. So I used the nz construct like ((nz([fieldx],10)-[param1])*0.255)^2). And it now works! Thanks for putting me on the right track guys.John Fowler

1 Answers

-1
votes

may be there is some nulls, try

where nz(expr4, 1) <1