Visible fields based on IIF logicals

mba_110

Registered User.
Local time
Today, 00:15
Joined
Jan 20, 2015
Messages
280
Hi everyone,


I struct with some logical calculations in coding, i have 5 fields on my report and their is 5 year selection so each iif condition should show result based on the year value.

for example

if txtYears equal to 1 than result should visible only [1 filed] and remaining 4 fields should be invisible on the report

if txtYears equal to 2.5 then result should visible [1 field] [2 field] [3 field] because its greater than 2 and the remaining 2 fields should be invisible.

by this way any number of txtYears should visible the fields based on value of txtYears.


I did some coding on report Onload event but only first condition is working not all.

Code:
Private Sub Report_Load()
If Me.Years <= 1 Then
me.field-1.visible=true
else
me.field-2.visible=false
me.field-3.visible=false
me.field-4.visible=false
me.field-5.visible=false
end if

If Me.Years > 1 <= 2 Then
me.field-1.visible=true
me.field-2.visible=true
else

me.field-3.visible=False
me.field-4.visible=false
me.field-5.visible=false
end if

exit sub
 
I'm surprised you don't get a syntax error:

Code:
If Me.Years > 1 <= 2 Then

That compares Me.Years to 1 and at the same time compares 1 to 2. Spoiler alert: 1 is always less than 2. You probably wanted to compare Me.Years to 2 as well, to do that you need to type out Me.Years again and use the AND keyword:

Code:
If Me.Years > 1 AND Me.Years<= 2 Then

With all that said, you can simplify your code a lot by setting all your fields to False, then just using a bunch of if statements to turn the ones you want on. No need to reference every single control in every single logic block.
 
So each one i have to put separate iif because as you mention only 2 fields are visible i need cut off of each based on Year value if 1 or 2 or 3.5 or 4.5 or 5 anything.

Sorry but not clear to understanding technically your advise.
 
In my report I would set the first field to Visible=True (since it seems it always shows) then the others I would set to Visible=False. Then my code would show the ones I needed


If Years>=2 Then Field2.visible=True
If Years>=3 Then Field3.visible=True
etc.

You really only need 5 lines of code to do this.
 

Users who are viewing this thread

Back
Top Bottom