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: - Private Sub Form_Load()
-
ListBox1.SetFocus
-
Me.ListBox1.Selected(0) = True
-
End Sub
Linq ;0)> 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. 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: - Private Sub Form_Load()
-
ListBox1.SetFocus
-
Me.ListBox1.Selected(0) = True
-
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? -
10-12-2007, 12:46 PM #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] -
10-12-2007, 04:14 PM #2 Djblois -- Try [VBA] Private Sub UserForm_Initialize() With Me.ListBox1 .Value = .List(0) End With
End Sub [/VBA]
Paul -
10-12-2007, 04:48 PM #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 -
10-15-2007, 08:04 AM #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] -
10-15-2007, 08:14 AM #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'? -
10-15-2007, 08:24 AM #6 Why are you making us guess Daniel? Please provide an example... Steve "Nearly all men can stand adversity, but if you want to test a man's character, give him power." -Abraham Lincoln -
10-15-2007, 08:48 AM #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. -
10-15-2007, 09:57 AM #8 Daniel So how is it 'crashing'?
What error message if any are you getting? -
10-15-2007, 10:42 AM #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-15-2007, 10:44 AM #10 Did you try this?
[vba]Me.lbColumn1SC.ListIndex = 0
[/vba] -
10-15-2007, 11:08 AM #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 -
10-15-2007, 01:02 PM #12 Daniel
Is listindex definitely in lower case? -
10-15-2007, 01:19 PM #13 No, I just checked it is "ListIndex" -
10-15-2007, 01:27 PM #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. Steve "Nearly all men can stand adversity, but if you want to test a man's character, give him power." -Abraham Lincoln -
10-15-2007, 01:33 PM #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. -
10-15-2007, 01:43 PM #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 "Nearly all men can stand adversity, but if you want to test a man's character, give him power." -Abraham Lincoln -
12-05-2007, 12:04 PM #17 Thank you... I needed this! -
12-06-2007, 05:14 AM #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 -
06-09-2017, 12:05 PM #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. -
06-09-2017, 01:10 PM #20 10 yo thread 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 |
|