Using "OR" in If...Then Statements

Mike Smith

Registered User.
Local time
Today, 14:20
Joined
Sep 22, 2000
Messages
20
I wrote the following code:

If FunctionName() = "ClarkL" Or "SmithM" Or "ThomasS" Then
Me.cmdEdit.Visible = True
End If

and get a Run-Time Error 13 Type Mismatch. Not sure what the error is indicating, so I rewrote the code:

If FunctionName() = "ClarkL" Then
Me.cmdEdit.Visible = True
ElseIf FunctionName() = "SmithM" Then
Me.cmdEdit.Visible = True
ElseIf FunctionName() = "ThomasS" Then
Me.cmdEdit.Visible = True
End If

which works fine. Is there a way to make the "Or" portion work and make the code smaller...or a better way alltogether?

Mike
 
Mike,

each test in the If statment needs to be a seprate expression, like so...

If FunctionName() = "ClarkL" Or FunctionName() = "SmithM" Or FunctionName() = "ThomasS" Then
    Me.cmdEdit.Visible = True
End If


however, you can also use a Select Case statment to achive the same effect to do a particlar action given a variable has one of a given list of values...

Dim strValue as String
strValue = FunctionName()

Select Case strValue
    Case "ClarkL", "SmithM", "ThomasS"
        Me.cmdEdit.Visible = True
    Case "Bob", "Bill"
        MsgBox "Hoorah!"
    Case "Bert"
        MsgBox "Wibble!"
End Select


Hope that helps.

axa
 
There's another way, but you'd never think to look it up unless you know what it is:

If FunctionName() in ("ClarkL","SmithM",ThomasS") then
' do whatever...

The magic word is "in".

axa's Case logic is also an excellent choice; depends on what you are doing.

[This message has been edited by Chris RR (edited 08-13-2001).]
 

Users who are viewing this thread

Back
Top Bottom