In MS Access VBA, I have different checks in different methods using different parameters. As soon as one of them fails, Cancel is set to True and no further checks should be executed. It looks like that:
Cancel = checkMethodOne(param1)
If Not Cancel Then
Cancel = checkMethodTwo(param2, param3)
End If
If Not Cancel Then
Cancel = checkMethodThree(param4)
End If
... and so on. Is there a way to code this more elegantly without the repeating "If Not Cancel Then" clauses?
Something like "While Not Cancel" seems a problem because of the different methods with individual names and parameters. Any ideas?
You can just use And
to evaluate multiple statements, and return false if any return false:
Cancel = checkMethodOne(param1) And checkMethodTwo(param2, param3) And checkMethodThree(param4)
If this is convenient for you might depend upon the situation. It's shorter and more code golf-y, but might be more confusing.
Note that this does evaluate all functions, so if they are performance-intensive it might run a bit longer
Alternatively, you can try the following Select case
to avoid executing all the comparisons:
Cancel = True
Select Case True
Case checkMethodOne(param1)
Case checkMethodTwo(param2, param3)
Case checkMethodThree(param4)
Case Else
Cancel = False
End Select