asked on
MS Access If statement on a report
If [AttributeValue] = "36" Then
[PDesc] = "Test"
End If
When I preview the report my unbound text box does not populate if the condition has been is met. What am I doing wrong?
ASKER
If Me![AttributeValue] = "36" Then
Me![PDesc] = "Test"
End If
ASKER
PriceList.zip
I would actually do that in the query that is the recordsource for the report:
Assuming AttributeValue is numeric:
SELECT Field1, Field2, ..., FieldN, IIF([AttributeValue] = 36, "Test", "") as Expr1 FROM your table
If AttributeValue is a string:
SELECT Field1, Field2, ..., FieldN, IIF([AttributeValue] = 36, "Test", "") as Expr1 FROM your table
And if you are going to have multiple options, you might consider using the Switch( ) function, which allows you to provide pairs of values, the first being an expression, the next being the value to return if the expression is true. That would look something like:
SELECT Field1, Field2, ..., FieldN
, Switch("[AttributeValue] = 36", "Test 36", "[AttributeValue] = 37", "Test 37", True, "Unknown Value") as Expr1
FROM your table
Notice that my final pair of arguments (True, "Unknown Value") are included so that if none of the other expressions evaluate to True, then the final pair will give you a value.
<< I would actually do that in the query that is the recordsource for the report:>>
I would disagree with Dale here; IIf() is not something I like to put in a query, but I would put this in a query by creating a description lookup table and and joining to it.
IIF()'s are very hard to write, troubleshoot, and maintain when they get more than a level or two deep. And with things like Switch(), Replace(), etc, again they can be difficult to write and maintain. Also some of these are VBA calls, which are not optimizable by the query processor and if you every move to a SQL backend, force a query to execute locally.
For something like this, you'd be far better off to do a lookup table and then a join to your main table.
Jim.
Well, Jim, he did only give one example.
So doing an IIF() or a Switch() would work for a relatively small set of AttributeValue values. But I would agree that a lookup table with AttributeValues and their descriptions would be far more efficient.
Dale ;-)
<< On Load event>>
You want this in the OnFormat of the section where the control is.
OnLoad only fires once at the start of the report, not for each record.
Jim.