Results 1 to 5 of 5
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    If but with or

    When using an IF in VBA



    as an example if I have

    IF StatusID = 3 then
    StatusID = 1

    end if

    but I want to also change other numbers, not just 3

    so 3 or 5 or 7 or 9 etc

    yet I'm not sure

    If StatusId = 3 or 5 or 7 then
    StatusID = 1
    end if

    The reason I ask as I want to shorten the code and not use elseif

    does any of this make sense?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You have to repeat the field:

    If StatusId =3 or StatusId =5 or StatusId =7 then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    ohhhh, ok that's easy enough. Thanks!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. I can't remember if In() works in VBA. It would in SQL.

    If Field In(3,5,7) Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    RayMilhon is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,069
    You can Also use Select

    Code:
    Select Case StatusID
        Case Is =3, is = 5, is = 7
                StatusID = 1
        Case Else
                StatusID = 2
    
    End Select
    Each group can have it's own Case For example

    Code:
    Select Case StatusID
         Case is = 3, Is = 5, Is = 7
                StatusID = 1
         Case is = 2, Is = 4, Is = 6
                StatusID = 2
         Case is > 7
                StatusID = 10
         Case Else
                StatusID = 0
    End Select

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums