Curious behavior of Access.Application.Eval() - ms-access

Explain why Access.Application.Eval() (commonly abbreviated as Eval()) produces a different result than just evaluating the original expression in this case:
Debug.Print Round(.575 * 100)
57
Debug.Print Eval("Round(.575 * 100)")
58
EDIT: To address GSerg's answer, the following still returns different results:
Debug.Print Eval("Round(CSng(.575) * 100)")
57
Debug.Print Round(CSng(.575) * 100)
58

That multiplication returns a different product under Eval(), but I don't understand why.
Debug.Print (.575 * 100) < 57.5
True
Debug.Print Eval("(.575 * 100) = 57.5")
-1
In the first case, the product is less than 57.5, so Round() will round it down to 57.
In the second case, the product is equal to 57.5, so Round() will apply its standard "round to even" approach to yield 58.
Edit: You all are right that Eval() coerces the literal value to a different data type.
? TypeName(.575)
Double
? Eval("TypeName(.575)")
Decimal
? Round(CDec(.575) * 100)
58

That is because of different float precision.
In one case the constants get recognized as Doubles, in the other as Singles.
? math.round((.575! - Int(.575!)) * 100)
58
? math.round((.575# - Int(.575#)) * 100)
57

GSerg got me thinking. I'm starting to believe that Jet attempts to coerce decimal literals to the Currency type when Eval is called whereas VBA coerces decimal literals to the Double type. Case in point:
? Math.Round(.575 * 100)
57
? Math.Round(CSng(.575) * 100)
58
? Math.Round(CDbl(.575) * 100)
57
? Math.Round(CCur(.575) * 100)
58
? Eval("Round(.575 * 100)")
58
? Eval("Round(CSng(.575) * 100)")
57
? Eval("Round(CDbl(.575) * 100)")
57
? Eval("Round(CCur(.575) * 100)")
58

I don't use Access, but I would guess that the Eval evaluates the expression as an Access expression, so that the Round and Int functions may operate differently from the VBA versions?
I know from bitter experience that VBA's Round() function uses the round-to-even rounding method (aka Banker's rounding), rather than the more common round-away-from-zero-on-a-5 (aka symmetric arithmetic rounding) method.

If you run the following SQL expression in Access, you get 58:
select Round((.575 - Int(.575)) * 100) as MyValue
If you run the following statement in Access (and, for that matter any Office VBA IDE) Immediate window, you get 57:
Round((.575 - Int(.575)) * 100)
So, that leads me to believe VBA has a different way of doing Round rather than Access and, probably more applicable, JET.
Now, why is that different? Dunno...will take someone with better skills than me.

Related

SSRS : Empty string from webservice errors even with iif statements

I am using a web service to get back a value. Which when not needed is empty string. However no matter what I do to try and code for if it is empty is always errors :
=iif( Fields!X.Value="","",iif(IsNumeric(Fields!X.Value) ,CStr(CDbl(Fields!X.Value) * 100),""))
Thanks for any help
Ian
Try the following...
=IIF(
IsNumeric(Fields.X.Value),
Val(Fields.X.Value) * 100,
""
)
If X is not numeric you will get and empty string, if X is zero you will get 0 returned and if X is numeric you will get X *100

Have DateDiff Show Decimals

I am using the DateDiff function, but I would like for it to give me 3 decimal places. How should my query be altered to achieve such result? -- I need this done via the query itself not a VBA function.
Date123: DateDiff('d', [startdate], [enddate])
For a line that you can just put into a query, I'd use something like the following.
Format(DateDiff("s",[DateOne],[DateTwo])/60/60/24,"#.###")
Better practice would be to create a function in a module in your db like the following. Then call it through the query. The code is more maintainable, testable and understandable.
Public Function DateDiffInFractions(DateOne As Date, DateTwo As Date) As String
Dim SecondsDiff As Double
SecondsDiff = DateDiff("S", DateOne, DateTwo) / 24 / 60 / 60 'Days/hours/minutes/seconds
DateDiffInFractions = Format(SecondsDiff, "0.000") 'Format to 3 Decimal points. Return string
End Function

Rails is escaping my query incorrectly

So I am trying to handle a fairly complex query within active record. I am using rails 4.0 and mysql. The problem is, my query requires single quotes in the WHERE statement.
This is what the sql statement (WHERE portion) needs to end up looking like:
WHERE
`location_hours`.`open_at` <=
hour(CONVERT_TZ(now(), 'US/Mountain',`locations`.`time_zone`)) * 60 * 60
AND
`location_hours`.`close_at` >=
hour(CONVERT_TZ(now(), 'US/Mountain', `locations`.`time_zone`)) * 60 * 60
The issue is with 'US/Mountain'. I have tried the following two ways to make this work:
1.
string = "hour(CONVERT_TZ(now(), 'US/Mountain', `locations`.`time_zone`)) * 60 * 60";
filtered = filtered.joins(:hours)
.where("`location_hours`.`closed` = ?", false)
.where("`location_hours`.`open_at` <= %s AND `location_hours`.`close_at` >= %s",
string, string)
2.
filtered = filtered.joins(:hours)
.where("`location_hours`.`day` = weekday(CONVERT_TZ(now(), \"US/Mountain\", `locations`.`time_zone`))")
.where("`location_hours`.`closed` = ?", false)
.where("`location_hours`.`open_at` <= hour(CONVERT_TZ(now(), \"US/Mountain\", `locations`.`time_zone`)) * 60 * 60 AND `location_hours`.`close_at` >= hour(CONVERT_TZ(now(), \"US/Mountain\", `locations`.`time_zone`)) * 60 * 60")
The sql the second option generates (copy,pasted from console) I can literally drop as a raw query into phpmyadmin, and I get results back.
For some reason Rails is doing something funny with my query. Any pointers or suggestions would be greatly appreciated!
Thanks!!
The easiest way to get single quotes into your SQL is to just put them in there:
.where("location_hours.open_at <= hour(CONVERT_TZ(now(), 'US/Mountain', ...
If the timezone is in a variable then use a placeholder:
.where("location_hours.open_at <= hour(CONVERT_TZ(now(), :tz, ...", :tz => time_zone_string)
Keep in mind that an SQL snippet such as
string = "hour(CONVERT_TZ(now(), 'US/Mountain', `locations`.`time_zone`)) * 60 * 60";
is not an SQL string and should not be escaped as such. That means that sending it through a %s won't do the right thing, the %s will just make a mess of the quotes in the snippet.
BTW, you don't need to backtick-quote everything, only identifiers that are keywords, case sensitive, contain whitespace, etc. Backticking everything just makes an ugly mess.

I can't search inside a text field for data with a hypen character

This query return nothing:
SELECT * FROM rates WHERE inks = "4 / 0 - [CMYK]"
But this one:
SELECT * FROM rates WHERE inks = "4 / 0 " + CHAR(150) + " [CMYK]"
or this one:
SELECT * FROM rates WHERE inks LIKE "4 / 0 % [CMYK]"
returns hundred of records. No matter if I use single or double quotes.
Why? I spend hours making different test but I can't get a reasonable explication.
You're likely using the wrong hyphen character in your first example.
For example, there's a difference between — (150 in Windows-1252) and – (151).
It could also be that your input data is UTF-8 encoded, in which case the hyphens wouldn't match either.
You can do :
SELECT * FROM rates WHERE inks LIKE "%-%";
It does not answer the why but the how ^^

How do metatables work and what are they used for?

I have this one question, about Lua metatables. I heard and looked them up, but I don't understand how to use them and for what.
metatables are functions that are called under certain conditions.
Take the metatable index "__newindex" (two underscores), when you assign a function to this, that function will be called when ever you add a new index to a table, like;
table['wut'] = 'lol';
this is an example of a custom metatable using '__newindex'.
ATable = {}
setmetatable(ATable, {__newindex = function(t,k,v)
print("Attention! Index \"" .. k .. "\" now contains the value \'" .. v .. "\' in " .. tostring(t));
end});
ATable["Hey"]="Dog";
the output:
Attention! Index "Hey" now contains the value 'Dog' in table: 0022B000
metatables can also be used to describe how Tables should interact with other Tables, and different values.
This is a list of all the possible metatable indexes you can use
* __index(object, key) -- Index access "table[key]".
* __newindex(object, key, value) -- Index assignment "table[key] = value".
* __call(object, arg) -- called when Lua calls the object. arg is the argument passed.
* __len(object) -- The # length of operator.
* __concat(object1, object2) -- The .. concatination operator.
* __eq(object1, object2) -- The == equal to operator.
* __lt(object1, object2) -- The < less than operator.
* __le(object1, object2) -- The <= less than or equal to operator.
* __unm(object) -- The unary - operator.
* __add(object1, object2) -- The + addition operator.
* __sub(object1, object2) -- The - subtraction operator. Acts similar to __add.
* __mul(object1, object2) -- The * mulitplication operator. Acts similar to __add.
* __div(object1, object2) -- The / division operator. Acts similar to __add.
* __mod(object1, object2) -- The % modulus operator. Acts similar to __add.
* __tostring(object) -- Not a proper metamethod. Will return whatever you want it to return.
* __metatable -- if present, locks the metatable so getmetatable will return this instead of the metatable and setmetatable will error.
I hope this clears things up, if you need a few more examples, click here.
They allow tables to be treated like other types such as string, functions, numbers etc.
For a high level, entertaining read on the prototype pattern check out http://steve-yegge.blogspot.com/2008/10/universal-design-pattern.html. This may help you with the 'what'.