Should multiple trues in a Switch function yield #Error?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I understand the switch function correctly, it processes the first
condition and if it is true then it returns the associated value and so on
through all the condition/value pairs in your function call. However, based
on my experience this evening with Access 2002, this does not appear to be
the case. If more than one of the conditions happens to be true, I'm getting
#Error instead of the value associated with the first true condition. Before
I start trying to recode the null string handling to elminate the multiple
trues, I wanted to see if this is consistent with the experience of other
users.

-- M.L. Cottingham
 
I don't see that problem in A2003.

Open the Immediate Window (Ctrl+G), and try:
? Switch(1=1, "Yes", 0=0, "Also true")

If that does not show the problem, then it would appear the problem is
elsewhere.
 
M.L. Cottingham said:
If I understand the switch function correctly, it processes the first
condition and if it is true then it returns the associated value and so on
through all the condition/value pairs in your function call. However, based
on my experience this evening with Access 2002, this does not appear to be
the case. If more than one of the conditions happens to be true, I'm getting
#Error instead of the value associated with the first true condition. Before
I start trying to recode the null string handling to elminate the multiple
trues, I wanted to see if this is consistent with the experience of other
users.


A function can only return a single value. Switch will
return the **first** value with a True condition.

Having multiple True conditions has nothing to do with a
result of #Error.

You should post a exact Copy/Paste of the Switch you're
using along with it's context and the values of the fields
in the function call.
 
Clearly... the problem isn't the multiple trues, so I'll rip into it in the
morning. Thanks for the quick feedback.

- M.L. Cottingham
 
Even though Switch will only return the value corrsponding to the first
True, it will evaluate *all* Boolean expressions in the arguments. If one or
more of these Boolean expressions error out, Switch will error out also.
 
I had already pulled each of the boolean expressions out and ran them
individually and they ran fine. Prompted by your post, I went back and
checked the return values. Even though they are not the first true, the
second and third true instance are returning #Error, since they are trying to
convert a NULL to a Value, which I had not coded around, since I expected
them to exit the switch before getting to that point.

THANKS for all the help.
-- M.L. Cottingham
 
Back
Top