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 Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Nested if Vs Switch
Chuck Corvec 
    
9 days ago
So I am old school and have been able to wrap my head around the switch function. I have watched the video's here and other places to no avail. In the next post is a screen shot of my Nested if. It is used to control access to tabs on a tabctrl. Is there a better way to fo this?
Chuck Corvec OP  @Reply  
    
9 days ago

Richard Rost  @Reply  
          
9 days ago
I see a problem with your Else line. Looks like it should be an ElseIf and the "then" is inside your quotes.

If you want to use the switch function, then use it. If not, there's nothing wrong with if-then-else if. I still use it all the time. I only revert to a switch statement when it's something very simple, like one-line items. If you've got blocks of code that need to go there, if-then-else is fine.
Richard Rost  @Reply  
          
9 days ago
Here's the proper If/Then:

DetailsIf uType = "developer" Then
    Me.MMTabCtrl.Pages(0).Visible = True
    Me.MMTabCtrl.Pages(1).Visible = True
    Me.MMTabCtrl.Pages(2).Visible = True
    Me.MMTabCtrl.Pages(3).Visible = True
ElseIf uType = "management" Then
    Me.MMTabCtrl.Pages(0).Visible = True
    Me.MMTabCtrl.Pages(1).Visible = True
    Me.MMTabCtrl.Pages(2).Visible = True
    Me.MMTabCtrl.Pages(3).Visible = False
ElseIf uType = "Data Entry" Then
    Me.MMTabCtrl.Pages(0).Visible = True
    Me.MMTabCtrl.Pages(1).Visible = True
    Me.MMTabCtrl.Pages(2).Visible = False
    Me.MMTabCtrl.Pages(3).Visible = False
End If


And here's the same logic using a Select Case (what most people call a switch):

DetailsSelect Case uType
    Case "developer"
        Me.MMTabCtrl.Pages(0).Visible = True
        Me.MMTabCtrl.Pages(1).Visible = True
        Me.MMTabCtrl.Pages(2).Visible = True
        Me.MMTabCtrl.Pages(3).Visible = True
    Case "management"
        Me.MMTabCtrl.Pages(0).Visible = True
        Me.MMTabCtrl.Pages(1).Visible = True
        Me.MMTabCtrl.Pages(2).Visible = True
        Me.MMTabCtrl.Pages(3).Visible = False
    Case "Data Entry"
        Me.MMTabCtrl.Pages(0).Visible = True
        Me.MMTabCtrl.Pages(1).Visible = True
        Me.MMTabCtrl.Pages(2).Visible = False
        Me.MMTabCtrl.Pages(3).Visible = False
End Select


Richard Rost  @Reply  
          
9 days ago
Where I find that select case statements come in handy is when it's one item per line, something like this...

DetailsSelect Case n
    Case 1: x = 5
    Case 2: x = 6
    Case 3: x = 10
    Case 4: x = 20
    Case Else: x = 0
End Select


And this would be the same thing with an if-then-else block...

DetailsIf n = 1 Then
    x = 5
ElseIf n = 2 Then
    x = 6
ElseIf n = 3 Then
    x = 10
ElseIf n = 4 Then
    x = 20
Else
    x = 0
End If


Functionally, it's the exact same thing, but it's just cleaner to read. It's completely up to you. Access doesn't care. VBA does not care which one you use.
Richard Rost  @Reply  
          
9 days ago
Make sure you watch Select Case
Kevin Robertson  @Reply  
           
9 days ago
Another option is Select Case.
Kevin Robertson  @Reply  
           
9 days ago
Richard You beat me to it. LOL.
Chuck Corvec OP  @Reply  
    
8 days ago
Richard Thanks bunches as my kids used to say when they was little. LOL I did not notice my type. That was what I intended. That's what happens when you type code in Notepad and don't make sure Access likes it.

Thank you
Chuck Corvec OP  @Reply  
    
8 days ago
Kevin I looked at that and decided to go with what I knew. I'll check here for a video on select case. NVM there is a link to it right in your comment! And Richard's. Man I am getting old.

Thanks again
Richard Rost  @Reply  
          
8 days ago
Chuck ain't we all. :)
Add a Reply Upload an Image
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: 4/29/2026 11:25:09 PM. PLT: 1s