Simple Iff statement problem (1 Viewer)

Joe8915

Registered User.
Local time
Yesterday, 23:34
Joined
Sep 9, 2002
Messages
820
I have a field called Invoice Number. If the Invoice Number contains the word final, then display a MsgBox "do something"
this is what I have so far.

Private Sub Invoice_Number_AfterUpdate()
IIf = (Invoice_Number) <> "final"
MsgBox ("then do something")
End Sub

Of course I get a error. Where did I go wrong?
 

boblarson

Smeghead
Local time
Yesterday, 22:34
Joined
Jan 12, 2001
Messages
32,059
IIF's are usually used in queries and control sources. IF statements are used in VBA.

Code:
Private Sub Invoice_Number_AfterUpdate()
   If Instr(1, Me.Invoice_Number, "final") > 0 Then
      MsgBox ("then do something")
   End If
End Sub
 

ddeford

New member
Local time
Today, 01:34
Joined
Sep 5, 2012
Messages
4
I have a field called Invoice Number. If the Invoice Number contains the word final, then display a MsgBox "do something"
this is what I have so far.

Private Sub Invoice_Number_AfterUpdate()
IIf = (Invoice_Number) <> "final"
MsgBox ("then do something")
End Sub

Of course I get a error. Where did I go wrong?

IIf can't be used in VBA code blocks. You'll need to use VBA for that. Here's the code you want:

Code:
IF txtInvoiceNumber <> "final" Then
    msgbox("text here")
END IF

If you're trying to do this in a control's Datasource property, then, yes, use IIf:

Code:
IIf ("final","text here",[txtInvoiceNumber])

the way IIf works:

IIF ([enter logic that will either be true or false], [do this if it's true], [do this if it's false])
 

boblarson

Smeghead
Local time
Yesterday, 22:34
Joined
Jan 12, 2001
Messages
32,059
IIf can't be used in VBA code blocks.
Not quite true. They can be used. It just isn't efficient since it checks both the true and false whereas the IF statement will stop checking when something matches.
 

Joe8915

Registered User.
Local time
Yesterday, 23:34
Joined
Sep 9, 2002
Messages
820
IIF's are usually used in queries and control sources. IF statements are used in VBA.

Code:
Private Sub Invoice_Number_AfterUpdate()
   If Instr(1, Me.Invoice_Number, "final") > 0 Then
      MsgBox ("then do something")
   End If
End Sub

It work like a charm
Thanks Bob
 

Users who are viewing this thread

Top Bottom