• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1148
  • Last Modified:

Access listbox: VBA to select an item

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
Jim Horn
Asked:
Jim Horn
  • 3
  • 2
2 Solutions
 
Rey Obrero (Capricorn1)Commented:
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
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Assuming a single select list box:

Me.YourListBoxName = "bananas"
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
btw ... is there just one column in the list box ?
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
There were five, but I butchered the example.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
lol ... no problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now