Solved

Access listbox:  VBA to select an item

Posted on 2013-01-14
6
960 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 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 - 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

823 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