Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where to put this darn IIf statement...

Status
Not open for further replies.

mpmoore

Technical User
Apr 5, 2001
27
US
I am trying to execute an immediate if based on a check box. I want certain fields to become invisible on my form if the box is not checked (true). Currently I have the code listed below in the "after update" control of the check box. When checked and exited, the box does nothing. When I uncheck the box, the questions apear like they are supposed to. Every other time I click the box, the macro executes. Where am I going wrong? I feel I am really close, but it is driving me crazy. Thanks, Michael Moore

IIf([Forms]![TheBigForm]![BusinessLease]=True,[Forms]![TheBigForm]![Option113].[Visible]-1,[Forms]![TheBigForm]![Option113].[Visible]=0)
 
You have a misunderstanding about IIf. It's not a statement, it's a function, and like any function it returns a value. It takes 3 arguments; if the first argument is true, the value returned is the value of the second argument, which is an expression. If the first argument is false, IIf returns the value of the third argument, which is another expression.

Notice that I'm stressing that the second and third arguments are expressions, not statements. It appears to me that you've coded what you thought were statements to be executed, but it happens that they are also valid expressions that compute (for the second argument) -2 if Option113 is visible, or -1 if it isn't, and (for the third argument) whether Option113 is invisible.

All your IIf function does is return one of these values. Normally, you code a function like this on the right side of an assignment statement. Since in this case you coded it "bare", the function result was calculated and then just discarded.

You need to turn this into a regular If statement that looks like this:
If [Forms]![TheBigForm]![BusinessLease] Then
[Forms]![TheBigForm]![Option113].Visible = True
Else
[Forms]![TheBigForm]![Option113].Visible = False
End If

In fact, you could code it much more cleanly:
If Me!BusinessLease Then
Me!Option113.Visible = True
Else
Me!Option113.Visible = False
End If

And still more efficiently:
Me!Option113.Visible = Me!BusinessLease Rick Sprague
 
Thanks for the clarification. You have been a tremendous help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top