November 14, 200223 yr I've noticed that a lot of users in this forum make frequent use of the "Case" where I probably would have used "If" Could someone exlain the difference, and why one might be prefered over the other? Thanks! -- Jason Wood HeyWoody.com
November 14, 200223 yr Hi: It might help to think of If like a logic statement. "If [ this, then that. Else this]." As you can see, If will only give you one of two results based on if the statement is true or not. Case, on the other hand, will evaluate the entire statement and will give results on the first value that is true, not just one result or the other. I hope that makes sense. Ken
November 14, 200223 yr Author I understand "If" fully from my QBasic days... maybe you could give me a syntax example for Case (I still don't quite get how it works). -- Jason Wood HeyWoody.com
November 14, 200223 yr Ok, let me try this. A typical case statement might look like this: Case (test1, result1, test2, result2, test3, result3, default result) If test1 qualifies, then you will get result1. If test2 qualifies, then you will get result2, and so on. If none qualify, you will get the default result. For example, suppose I have a field (we'll call it Num) that will be populated with numbers 1 through 4. I want a calc field (call it NumCalc) that will change the number to be spelled out ( "1" would be "one"). Sorry its a silly example, but bear with me. I would set up the NumCalc field like this: Case (PatternCount(Num, "1"), "one" , PatternCount(Num, "2"), "two", PatternCount(Num, "3"), "three", PatternCount(Num, "4"), "four", "five"). Just think of it as an If statement with more than one test. Does that help?? Ken
November 14, 200223 yr Author I had to lookup PatternCount() but now it all makes sense! I had been using nested If statements to achieve these results, so I sure am glad I asked! -- Jason Wood HeyWoody.com
November 15, 200223 yr That is the main reason for using Case statements. Nested If's can be horrible to debug. Apparently Case statements execute faster too.
Create an account or sign in to comment