Using "IIf" or "In" in VBA

J

Jim Pockmire

I believe there is a way to use these functions in VBA, but I ca't remember
how to do it.
 
L

Larry Linson

Jim Pockmire said:
I believe there is a way to use these functions in VBA, but I ca't remember
how to do it.

This will work,

somevar = IIF(avar = something, retvalfortrue, retvalforfalse)

but most think it easier to follow, later, if you use

If avar = something Then
somevar = retvalfortrue
Else
somevar = retvalforfalse
End If

or the alternative

IF avar = something Then somevar = retvalfortrue Else somevar =
retvalforfalse

FOR EACH varname IN arrayorgroup

is one valid use of IN in VBA. (Open the code window, type "In" in a module,
put the cursor on it, press F1, and that's the Help topic.)

Larry Linson
Microsoft Access MVP
 
D

Dirk Goldgar

Jim Pockmire said:
I believe there is a way to use these functions in VBA, but I ca't
remember how to do it.

By "In", are you referring to the SQL In operator, as "WHERE fieldname
In (1, 2, 3, 4)"? AFAIK, that operator doesn't exist in VBA, though
there are several ways to do the equivalent. One is the Select Case
statement; e.g.,

Select Case expression
Case 1, 2, 3, 4
' ... do something ...
Case 5, 6, 7, 8
' ... do something else ...
Case Else
' ... do the default thing ...
End Select
 
M

Marshall Barton

Jim said:
I believe there is a way to use these functions in VBA, but I ca't remember
how to do it.

There is an IIf function in VBA and a very similar, but not
quite identical, function in SQL. Help explains the VBA
function fairly well.

In is an SQL operator, but it is not avaliable in VBA.
However you can use it in the Eval function.

y = Eval( x & "IN (1,2,3,4)" )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

IIf question 7
hide zeros 2
Excel VBA 0
VBA Quotes using IIF 4
BeforeUpdate Event 3
Iif Form To Table 3
Replacing a sentence with VBA-code 2
Add one year in second combobox in vba 0

Top