Solved

Access listbox:  VBA to select an item

Posted on 2013-01-14
6
998 Views
Last Modified: 2013-01-14
Hi guys

I have an Access list box that populates fine.  No rows are selected.
Example:   "apples", "oranges", "bananas", "watermelon".

Question:  What is the VBA code to programmatically select an item (say "bananas") in the list box?  

I see the .ItemsSelected property, but it seems to only apply to all items selected.  

Thanks in advance.
Jim
0
Comment
Question by:Jim Horn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 250 total points
ID: 38775912
try this in the load event of the form

Private Sub Form_Load()
Dim j
With Me.List0
    For j = 0 To .ListCount - 1
        If .Column(0, j) = "bananas" Then
            .Selected(j) = True
            Exit For
        End If
    Next
End With
Me.List0.SetFocus
End Sub
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 250 total points
ID: 38775916
Assuming a single select list box:

Me.YourListBoxName = "bananas"
0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 38775964
Both work, although I went with Cap's as I forgot to mention that while I have "bananas", the bound column in the listbox was basically the FruitID.  Thanks.  -Jim
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75
ID: 38775967
btw ... is there just one column in the list box ?
0
 
LVL 65

Author Comment

by:Jim Horn
ID: 38775988
There were five, but I butchered the example.
0
 
LVL 75
ID: 38776009
lol ... no problem.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question