Choose ImageField.Picture based on the Value in Another Field

1.7k views Asked by At

I am new to Access but, thanks to this great forum, I have learned a lot in the past few weeks.

I am trying to make an image control to display one of three arrow images depending on a value of "1,2 or 3" in another field. The arrows represent market trends for the current record. The arrows are Up (value = 1, image="cmdTrendUp"), Sideways (value = 2, image="cmdTrendNeutral") and Down (value = 3, image="cmdTrendDown"). (The images are labeled "cmd" because the same images are shared with buttons on another form.)

I used nested IIF functions as an expression. The image for a value of "1" shows up as expected. However when I cycle through the records only the Green/Up arrow is displayed. Can you help me determine what is wrong?

=IIf([DEMO_UrlMsaCountyCAmq_MuniList]![TrendarrowValue]=1,[cmdTrendUp],IIf([DEMO_UrlMsaCountyCAmq_MuniList]![TrendarrowValue]=2,[cmdTrendNeutral],IIf([DEMO_UrlMsaCountyCAmq_MuniList]![TrendarrowValue]=3,[cmdTrendDown],[Anicon Orb.png])))
2

There are 2 answers

4
E Mett On

Use the Choose function

Quoted from the Office Help:

Syntax Choose(index, choice-1[, choice-2, ... [, choice-n]])

The Choose function syntax has these parts: Part Description index Required. Numeric expression or field that results in a value between 1 and the number of available choices. choice Required. Variant expression containing one of the possible choices. Remarks

Choose returns a value from the list of choices based on the value of index. If index is 1, Choose returns the first choice in the list; if index is 2, it returns the second choice, and so on. You can use Choose to look up a value in a list of possibilities. For example, if index evaluates to 3 and choice-1 = "one", choice-2 = "two", and choice-3 = "three", Choose returns "three". This capability is particularly useful if index represents the value in an option group. Choose evaluates every choice in the list, even though it returns only one. For this reason, you should watch for undesirable side effects. For example, if you use the MsgBox function as part of an expression in all the choices, a message box will be displayed for each choice as it is evaluated, even though Choose returns the value of only one of them. The Choose function returns a Null if index is less than 1 or greater than the number of choices listed. If index is not a whole number, it is rounded to the nearest whole number before being evaluated.

Example This example uses the Choose function to display a name in response to an index passed into the procedure in the Ind parameter.

Function GetChoice(Ind As Integer) GetChoice = Choose(Ind, "Speedy", "United", "Federal") End Function

© 2010 Microsoft Corporation. All rights reserved.

3
E Mett On

Create a table with autonumber field and an image field. Store the images there with their respective indexes.

Add this table to the underlying query using the index(1,2,3) as a join.

Bind the image control to the image field in the underlying query.