MS Access, Use Expression Builder to Compare Field in One Table to DLookup in Another Table - ms-access

I'm trying to make a MS Access report, where I use a text box to display a field value, then have another text box indicating if the first value is higher or lower than an entry in a separate table.
The report has a record source of "Table 1", and a textbox named "txt_Value1" which displays the number in Field: "Value1". I have a second table, "Customer_Criteria" which has a field "PassValue" that I want to compare against. My expression builder statement is:
IIf([txt_Value1]<(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'")),"TRUE","FALSE")
This statement always returns false, regardless of what the correct logical result is.
I've tested it, writing:
IIf(1<(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'")),"TRUE","FALSE")
And I get the correct results. Also, if I write:
IIf([txt_Value1]< 1,"TRUE","FALSE")
I get the correct results. What am I missing to compare the textbox value vs. the Dlookup?

As I understand, both fields are numeric. Access may consider those fields as text, so for correct comparing use type conversion.
Try this:
IIf(CLng(Nz([txt_Value1],0))< _
CLng(Nz(DLookUp("[PassValue]","[Customer_Criteria]","[Customer] = 'ABC'"),0)), _
"TRUE","FALSE")
Nz required if fields may contain NULL values, in this case type conversion function will return error.

Related

SSRS lookup missing dataset

I have a dataset which looks like this
Name Spend
"First Aid" 2
"Healing Arts" 0
"Surgeon" NULL
I then have three separate textboxes which will be filled with the value of the column which matches the name.
Example: show value of spend in textbox if value of name equals First Aid
for this I've made following expression
=Lookup(Fields!skill_name.Value, "First Aid", Fields!skill_spend.Value, "Skills")
My problem is however that I get an error saying that skill_name is missing its dataset, which doesn't make sense to me as it is informed in the end of the expression (skills)
I think you may be misunderstanding the purpose of Lookup and how it is used. The purpose of the Lookup function is akin to a JOIN in SQL in some ways. Basically, you would have two datasets that each have a matching field with the other. In that scenario, the expression would match on the skill_name field and lookup the skill_spend value and the expression would look something like the following.
=Lookup(Fields!skill_name.Value, Fields!skill_name.Value, Fields!skill_spend.Value, "Skills")
As the documentation shows, the first reference to skill_name is the field you are referencing from the current dataset. The second reference is to the dataset from which you are attempting to look up a value. The third expression is the field you are looking up and the dataset should be the one you are attempting to look up a value from, not the current dataset scope.
Lookup(source_expression, destination_expression, result_expression, dataset)
From the best I can tell, you have a single dataset but separate textboxes that need the correct spend value. I think the following expression will work.
= IIF(Fields!skill_name.Value = "First Aid", Fields!skill_spend.Value, Nothing)
This expression should get the skill_spend value associated with the row "First Aid" only and leave the textbox blank otherwise.

Sum Values not equal to a space from a Control Source in MS Access

As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))

MS ACCESS formatNumber in Calculated Field

I have a uID field linked to an auto number field Num that generates unique, custom numbering for each record in a table.
However the Expression used for the Data type keeps returning an error.
If Field1 = "Cat" then field (uID) must return "C" & [Num]. With the number format "000". ie C001, C010, C121 etc
IIf([Field1]="Cat","C" & formatNumber([Num],3) & [Num],"Unknown")
It seems Access doesn't recognise formatNumber used in this manner.
put this code in your textbox control source
=IIf([Field1]="Cat";"C"+Format([NUM];"000");Null)
this will give you the output what you need.

Passing a report variable to another expression

I'm trying to pass a report variable to an expression used to populate textbox but it's giving me syntax error. Here's the expression.
=Fields!Variables!col1.Value.Value
The variable (Variables!col1.Value) should return a fieldname that I want to use to populate a textbox inside a table.
Edit:
Basically I have a multi-value parameter that passes on to a stored procedure that pulls the data based on the parameter value. The first 4-5 columns returned are static while the rest are dynamic and can have unknown number of columns but I do know the max number of columns.
What I'm trying to do is to create a fixed number of columns and then show/hide them based on the parameter. I get the column title via an expression (=Parameters!Course_IDs.Label(0)) and then I want to use this value to create a textbox that will populate the column based on this value Course_IDs.Label(0).
Since it is a multi-valued parameter, you will likely need to join the results:
=JOIN(Parameters!ParameterName.Value, ", ")
If it is a variable from the dataset you will likely need to use LOOKUPSET() to get the values.
Also note that parameter values might be ugly to look at. In that case use the name instead - Parameters!ParameterName.Label

SSRS expression with missing column not working

I'm trying to create an expression with a missing column. as in the column is not returned from the query but does exist in the list of fields.
The problem I'm having is that every time I do an expression and one of the parameters is from a field that doesn't have a return from query the query fails silently. the following example is looking at a field "test" that, as I said, exists in the list of fields but is not returned from the query, how can I have this is statement send "alert"??
=IIF(Fields!test.IsMissing,"alert",Fields!test.Value)
The reason that I don't return the field is that the columns are dependent on the parameters I enter in the procedure (so they could be used or not depending on what the user is asking)
Thank you
From reading your question I think, you have dynamic columns which will be returns conditionally
So what you should do is,
1) Create the parameter in the parameter list and set it as the internal and assign the field value to that parameter, suppose the parameter is dyanmicfiledvalue
2) Change your expression as,
=IIF(IsNothing(Parameters!dyanmicfiledvalue.value),"alert", Parameters!dyanmicfiledvalue.value )
that should do it. Let me know in case of issues.
Or if you want to check null or empty value for that column just change as
=IIF(IsNothing(Fields!test.value),"alert",Fields!test.value)