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 if Statement in Regard to a List Box
Robert Blanchette 
    
15 months ago
I'm trying to do something that I thought would be easy but...
I have a list box in a form in which I have 5 items to choose from.
In VBA, I would like to send a msgbox if no item has been selected.
I thought something like

If MyListBox is null then
Msgbox

Or maybe
If MyListBox not in (1,2,3,4,5) then
Msgbox

It looks like this is not the way to do it.
I appreciate any suggestions.
Thanks
Sami Shamma  @Reply  
             
15 months ago
Please be more specific. See Rule #2.

You want to msgbox if no items were displayed ?
Adam Schwanz  @Reply  
           
15 months ago
U mean something like this?
    Dim i As Integer
    Dim MySelected As String
    
    For i = 0 To MyList.ListCount - 1
        If MyList.Selected(i) Then
            MySelected = MySelected & MyList.ItemData(i) & vbCrLf
        End If
    Next i
    
    If Len(MySelected) > 0 Then
        'have selections, list them
        MsgBox MySelected
    Else
        'none selected
        MsgBox "None Selected"
    End If
Thomas Gonder  @Reply  
      
15 months ago
@Adam I see were still using "i" from my oldest of days in BASIC. I was, back then, taught not to do a calculation in the upper limit of the For statement, to instead use a variable (as to avoid repeating the calculation i times). Does anyone know if this is still true in VBA?
Kevin Robertson  @Reply  
          
15 months ago
This will return exactly the same values as Adam's code but is more compact.

    If Not IsNull(MyList) Then
        MsgBox MyList.ItemData(MyList.ListIndex)
    Else
        MsgBox "None Selected"
    End If


Both are valid.
Robert Blanchette OP  @Reply  
    
15 months ago
Thanks very much to all of you. It did help me do what I wanted.  I appreciate the time you spent helping me.

Next time, I'll try to be more specific and complete from the start. Thanks again

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/8/2026 3:35:05 AM. PLT: 0s