• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1177
  • 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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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