Access VBA listbox select first item

Actually, since the index is Zero-based, it would be (0) not (1) for the first item, and I believe you need to set focus to the listbox first:

  1. Private Sub Form_Load()
  2.   ListBox1.SetFocus
  3.   Me.ListBox1.Selected(0) = True
  4. End Sub

Linq ;0)>


Access VBA listbox select first item
Access VBA listbox select first item
Access VBA listbox select first item

3 20898

munkee

374 256MB

Me.ListBox1.Selected(1) = True

Where listbox1 is the name of your listbox and (1) denotes the item number in your listbox to select.

Access VBA listbox select first item

missinglinq

3,532 Expert 2GB

Actually, since the index is Zero-based, it would be (0) not (1) for the first item, and I believe you need to set focus to the listbox first:

  1. Private Sub Form_Load()
  2.   ListBox1.SetFocus
  3.   Me.ListBox1.Selected(0) = True
  4. End Sub

Linq ;0)>

JohnL

Wow -- this simple post ended a long journey of frustration for me today! Awesome! If you have a listbox on a Tabbed Form (not subform) - see the following: I was using the Listbox as a navigation tool to select a records. Each tab's Listbox had a different query associated with it so it had different recordsets. When I would click on the tab it wouldn't change to the new recordset associated with the listbox for that tab/page. You must do the following to force the new recordset to be selected thus displaying the correct records. Note you must use an on change event for the tab control NOT the page. If you use an event associated with the page -- it is referencing the blank part of that page -- not the tab. Note: Tab control value is the page index it starts are zero not 1. So the example below is for my Page Index 1 which is the second page. Use the following for example: Private Sub TabCtl0_Change() If TabCtl0.Value = 1 Then List1439.SetFocus Me.List1439.Selected(0) = True End If

End Sub

Sign in to post your reply or Sign up for a free account.

In an Access form, I'm using a listbox to select the record I want to view (which is working fine, manually). The listbox is populated with names in a query that sorts them alphabetically, and that works fine.

Using VBA, I have the listbox selecting the first item in the box both when the form first loads, and when the user clicks a button to requery (the user can narrow down the list of names, by age, on the form itself.

When the form first loads, it has no trouble loading the record of the first item in the listbox. When the Search button is pressed, the form loads the first record in the database that matches the criteria, even if it doesn't match the first item in the listbox.

This is my code for both events:

Private Sub btnSearch_Click() DoCmd.RunMacro "Requery", 1 Me.listControl.SetFocus Me.listControl.Selected(0) = True Me.listControl = Me.listControl.ItemData(0) End Sub Private Sub Form_Load() DoCmd.RunMacro "Requery", 1 Me.listControl.SetFocus Me.listControl.Selected(0) = True Me.listControl = Me.listControl.ItemData(0) End Sub

What am I doing wrong?

  1. #1

    I am trying to select the first item in a list box with its index number. This is what I have tried and it keeps crashing: [vba]Me.lbColumn1SC.ListIndex.Value = 1[/vba] The reason why I am trying it this way is because the list is always different so I can't select in by name. I even tried:

    [vba]Me.lbColumn1SC.ListIndex = 1[/vba]

  2. #2

    Djblois -- Try [VBA] Private Sub UserForm_Initialize() With Me.ListBox1 .Value = .List(0) End With End Sub [/VBA]

    Paul

  3. #3

    If the list box is on a Userform or from the Toolbox,

    Me.ListBox1.Selected(0) = True

    If its from the Forms menu, its 1 based and this syntax is needed

    ActiveSheet.Shapes("List Box 1").OLEFormat.Object.Selected(1) = True

  4. #4

    This Crashes the program: [VBA]

    Me.ListBox1.Selected(0) = True

    [/VBA] and this one won't compile:

    [VBA]With Me.ListBox1

    .Value = .List(0)

    End With[/VBA]

  5. #5

    Can you tell us where the listbox is actually located? When are you actually trying to do this? Is the listbox actually populated when you try it?

    How is it 'crashing'?

  6. #6

    Why are you making us guess Daniel? Please provide an example...

    Steve

    Access VBA listbox select first item
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."

    -Abraham Lincoln

  7. #7

    Sorry, It is in a form. It is populated already. I add the data to it before I show the form with this: [VBA]PivotSummary.lbColumn1SC.List = Array("Product", "Division", "Department", "Country", _ "P-Category", "P-Manager")[/VBA] However, I am trying to add this in the forms code window:

    [VBA]With Me.ListBox1

    .Value = .List(0)

    End With[/VBA]

    The reason I am trying to add it there is I use the same form for multiple reports. Ex: Customer Reports and Product Reports and even though I use this to chose one of the items in the list before I show the form, nothing is selected: [VBA]PivotSummary.lbColumn1SC.Value = "Product"[/VBA]

    so I am trying to get it selected other ways.

  8. #8

    Daniel So how is it 'crashing'?

    What error message if any are you getting?

  9. #9

    "Could not select the selected Property. Invalid property value." and to double check I bypassed the line and it was populated.

    However, if I change the True to False then it doesn't crash. I also tried a value of 1 and it still crashes with the same error message.

  10. #10

    Did you try this? [vba]

    Me.lbColumn1SC.ListIndex = 0
    [/vba]

  11. #11

    no but I tried [VBA]me.lbcolumn1SC.listindex = 1[/VBA] and that also crashed with a error saying:

    Could not select the set the List Index Property. Invalid property value

  12. #12

    Daniel

    Is listindex definitely in lower case?

  13. #13

    No, I just checked it is "ListIndex"

  14. #14

    since you don't seem to be willing to post an example....Paul's method works so you're doing something different which we can't see because......I just don't understand.
    Access VBA listbox select first item
    Access VBA listbox select first item
    Access VBA listbox select first item

    Steve

    Access VBA listbox select first item
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."

    -Abraham Lincoln

  15. #15

    Lucas,

    the underlying code is so large and ingrained in the program it would take me about 3 hours to post an example. I am not trying to make it any harder, just to show what I am doing fully would take a long time.

  16. #16

    I don't know what to say.....did you try the example that I posted with Paul's code? Did it work for you in the example....if so why can't you incorporate it into your code?

    Steve

    Access VBA listbox select first item
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."

    -Abraham Lincoln

  17. #17

    Thank you... I needed this!

  18. #18

    Daniel, You do have the

    me.lbcolumn1SC.listindex = 0
    line after the line that populates the listbox, don't you? (If not, you will get the error you describe)

    Regards, Rory

    Microsoft MVP - Excel

  19. #19

    My experience with ListBox controls in UserForms is that setting .ListIndex = n or .Selected(n) = True doesn't seem to help at all. The only workaround that I've found is the following, where "n" is the list item to be selected/highlighted:

    Dim SaveVal As String With MyUserForm.SomeListBox SaveVal = .List(n) .Value = "" .Value = SaveVal .SetFocus 'Not required, but allows up/down arrow keys to function normally. End With

    Note that just re-assigning the selected list item to its current value doesn't work because the underlying code apparently checks for that case and treats it as a no-op.

  20. #20

    10 yo thread
    Access VBA listbox select first item

    I expect the student to do their homework and find all the errrors I leeve in.

    Please take the time to read the Forum FAQ