Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 1st, 2007, 08:45 AM
Authorized User
 
Join Date: May 2007
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mulitiple/Nested IIF statements

Hi,

Can anyone tell me the right way of writing multiple/Nested IIF statements. I am having a problem with Syntax. Also how many multiple IIF statements can i right in an expression. I am asking this because i have to change a field couple of times based on several conditions......with a single expression which has multiple IIF statements......also can somebody let me if this not possible in SQL is there a way to do this in VBA and add it to the query...to add to it i am not using a form to run this query....

Reply With Quote
  #2 (permalink)  
Old May 1st, 2007, 11:18 AM
Friend of Wrox
 
Join Date: May 2006
Location: Jonesboro, AR, USA.
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can use an Else If statement in VBA like the one below. If you just use a query you can build it into the criteria.

Code:
    If sLine = "11ALR" Then
        DoCmd.OpenQuery "qry_11ALR"
        DoCmd.OpenQuery "Q_11ALR2"
    ElseIf sLine = "11BLR" Then
        DoCmd.OpenQuery "qry_11BLR"
        DoCmd.OpenQuery "Q_11BLR2"
    ElseIf sLine = "11CLR" Then
        DoCmd.OpenQuery "qry_11CLR"
        DoCmd.OpenQuery "Q_11CLR2"
    ElseIf sLine = "11DLR" Then
        DoCmd.OpenQuery "qry_11DLR"
        DoCmd.OpenQuery "Q_11DLR2"
    ElseIf sLine = "11ELR" Then
        DoCmd.OpenQuery "qry_11ELR"
    ElseIf sLine = "12ALR" Then
        DoCmd.OpenQuery "qry_12ALR"
        DoCmd.OpenQuery "Q_12ALR2"
    ElseIf sLine = "12BLR" Then
        DoCmd.OpenQuery "qry_12BLR"
        DoCmd.OpenQuery "Q_12BLR2"
    ElseIf sLine = "12CLR" Then
        DoCmd.OpenQuery "qry_12CLR"
        DoCmd.OpenQuery "Q_12CLR2"
    ElseIf sLine = "14ALR" Then
        DoCmd.OpenQuery "qry_14ALR"
        DoCmd.OpenQuery "Q_14ALR2"
    ElseIf sLine = "14BLR" Then
        DoCmd.OpenQuery "qry_14BLR"
        DoCmd.OpenQuery "Q_14BLR2"
    ElseIf sLine = "14CLR" Then
        DoCmd.OpenQuery "qry_14CLR"
        DoCmd.OpenQuery "Q_14CLR2"
    End If


Reply With Quote
  #3 (permalink)  
Old May 1st, 2007, 11:22 AM
Friend of Wrox
 
Join Date: May 2006
Location: Jonesboro, AR, USA.
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is a nested IF statement in VBA. It also contains a Do Loop.
Code:
        If iMax <> "" Then
            rsCur.MoveFirst
            Do Until rsCur.EOF
                sLimit = Mid(rsCur![CURRENT1], 5, 4)
                iLimit = CInt(sLimit)
                If sLine = "11ALR" Or sLine = "11CLR" Or sLine = "11DLR" Or sLine = "11ELR" Or sLine = "12ALR" Then
                    If iLimit > iMax Then
                        rsCur.Edit
                        rsCur![CURRENT1] = Null
                        rsCur.Update
                    End If
                Else
                    If iLimit < iMax Then
                        rsCur.Edit
                        rsCur![CURRENT1] = Null
                        rsCur.Update
                    End If
                End If
                rsCur.MoveNext
            Loop
        End If


Reply With Quote
  #4 (permalink)  
Old June 1st, 2007, 10:44 AM
pjm pjm is offline
Authorized User
 
Join Date: Jul 2006
Location: Boston, MA, USA.
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I believe that the limit to any expression in a query is 255 characters. Nesting in IIF is fairly straight-forward (although it can make your eyes go bleary).

       IIF( condition1, IIF( condition2, IIF( condition2b, answer, answer), IIF( condition2c, answer, answer) ), IIF( condition3, answer, answer) )

       and so on ...

Just get those pesky parentheses and commas right!


-Phil-
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple IIF Statements rohit_ghosh Access 2 May 7th, 2007 03:05 PM
Nested IIF rohit_ghosh Access 5 May 4th, 2007 09:29 AM
Multiple IIF statements Corey Access 1 November 16th, 2006 05:25 PM
Nested IIF Statement Help Needed Corey Access 4 November 17th, 2005 10:19 AM
Nested Query with an iif statment Corey Access 7 October 17th, 2005 04:40 PM



All times are GMT -4. The time now is 12:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.