Full Version: Referring to variable in an IIF statement
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
soldier
I am unable to refer to a variable in an IIF statement.

Here is what I have done. I declared the variable in a module.
Public strUnused As String

I set it to True when the form opens.
strUnused = True

After a combo is run it is set to False
Sub Combo247_AfterUpdate()
strUnused = False

The form uses If/End If code.
Private Sub Combo15_AfterUpdate()
If strUnused = True Then
Command12.Visible = False
Else
Command12.Visible = True
End If
End Sub
strUnused works perfectly in the above code.

Now, the problem.

I created a Text Box and for Control Source I used an IIF statement.
=IIf([strUnused]=True,"A","B")
The Text Box shows an error - #Name?

I put quotes around True.
=IIf([strUnused]="True","A","B")
The Text Box still shows the error - #Name?

I try to remove the square brackets but they keep coming back in.
=IIf(strUnused=True,"A","B")

I would appreciate it if someone can tell me how to refer to this variable in the IIF statement.

Thank You!!
theDBguy
Hi,

If you want a true or false value, you should probably declare the variable as a Boolean. For example:

Public strUnused As Boolean

To access the variable in an IIf() statement, you probably need to create a function to retrieve it. For example:

Public Function GetstrUnused() As Boolean
GetstrUnused = strUnused
End Function

Then, you could use:

=IIf(GetstrUnused(), "A", "B")

Hope that helps...
soldier
It did what I needed.

I learned a lot in your little tutorial;

1) the function as Boolean
2) using the function in IIF

Thanks a lot!!!

Ken
theDBguy
You're welcome, Ken. Glad we could help. Good luck with your project.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.