Solved

Access listbox:  VBA to select an item

Posted on 2013-01-14
6
940 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
  • 3
  • 2
6 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero 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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now