Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
VBA Question
Sandra Truax 
         
3 years ago
This works, but is there a simpler, more compact way to write this code?

    If AccountID = 11 _
        Or AccountID = 20 _
        Or AccountID = 8 _
        Or AccountID = 18 _
        Or AccountID = 9 _
        Or AccountID = 12 Then
            A = -1
            ST = 0
    Else
        A = 0
        ST = -1
    End If
Adam Schwanz  @Reply  
           
3 years ago
You could use CASE

Then you could do Case 11,20,8,18,9,12

Adam Schwanz  @Reply  
           
3 years ago
Looks like Rick has some case in the ext cut here
Switch Function
Sandra Truax OP  @Reply  
         
3 years ago
Thanks Adam.  I watched that video this morning, and tried to figure out the code, but couldn't.  I've never been able to get CASE to work for me.  Since I now know I was on the right track, I will try again.
Sandra Truax OP  @Reply  
         
3 years ago
After watching Switch Function again, and Access Developer 2 again, I still can't get CASE to work, so I give up. I guess I'll just stick to my original code since it works.
Kevin Yip  @Reply  
     
3 years ago
You can store all those values in an array, then use a loop to check the values in the array, so you don't need to check them individually (see code below).  If you only have a few values to check, then your original code is fine.  If you have a lot, then the code below is more efficient -- not only is it less repetitious, but it also makes it easier to add additional values in the future should you need to.

    Dim arr As Variant, i As Integer
    arr = Array(11, 20, 8, 18, 9, 12, ... ...)
    A = 0: ST = -1
    For i = 0 To UBound(arr)
        If AccountID = arr(i) Then A = -1: ST = 0
    Next i

I put A=0: ST=-1 at the top, removing the need to use Else.  If AccountID doesn't match any values, then A and ST will remain 0 and -1.

The UBound() function returns the size of the array.  It is zero-based.  If arr has 6 numbers, then UBound(arr) equals 5.

The use of colon (:) in the If Then statement removes the need to have End If, shortening the code even more.  But only use colon when the code is short like that, so it won't hurt readability and manageability.
Sandra Truax OP  @Reply  
         
3 years ago
Thank you Kevin Yip!  That works, and the accounts do change pretty often, so this will make it easy to update them.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 12:15:07 PM. PLT: 1s