Solved

ListIndex error 7777 You've used the ListIndex property incorrectly

Posted on 2009-05-11
8
4,199 Views
Last Modified: 2013-11-28
I am trying to running the following code, to select the list box value where there is only one item in the list. However I keep getting the error "7777 You've used the ListIndex property incorrectly". I have observed the following points.

1. I have SetFocus to the control that I have tried set the value for.
2. I have tried to use the selListBox.Selected(0) method, but keep getting the "Object deosn't support this property or method"
3. The selListBox is passed as a variant because it is passed from a collection. I have changed this to selListBox as ListBox - but the same error occurs.
4. Although the error message ""7777 You've used the ListIndex property incorrectly" occurs,  the correct list box item does get selected.

I am using Access 2003. Any suggestions? Thanks
Function SelectOneListItem(selListBox As Variant)
 

If selListBox.ListIndex = -1 Then

    If selListBox.ListCount = 1 Then selListBox.SetFocus: selListBox.ListIndex = 0: selListBox.Selected(0) = True
 

End If

End Function

Open in new window

0
Comment
Question by:HeronandBrearley
  • 4
  • 3
8 Comments
 
LVL 65

Accepted Solution

by:
rockiroads earned 250 total points
ID: 24353154
Not sure why u need to check the ListIndex
You can use IemData to refer to the first item

Function SelectOneListItem(selListBox As Variant)
 
    If selListBox.ListCount = 1 Then
        selListBox.SetFocus
        selListBox.value = selListBox.itemdata(0)
    End If
End Function

0
 
LVL 84
ID: 24353159
don't use ListIndex; just set the value directly:

selListBox = 0

the Selected method expects a True or False:

selListbox.Selected(0) = True

And I'd encourage you to avoid the colon method of code; place each of your statements on a individual line. It's MUCH easier to read, and will be much appreciated by any developer who comes behind you to work on your app (or by yourself when you must make changes 6 months from now). it's difficult to scan through code and find what you're looking for with code constructed in this manner.
0
 
LVL 84
ID: 24353164
Also, the correct item is "selected" since you're setting the focus to the listbox, which will select the FIRST item (i.e. the 0 item).
0
 

Author Comment

by:HeronandBrearley
ID: 24353218
LSMConsulting:

Thanks for the response - after all the swapping and changing code to check if various methods work, I have copied the worng code into my question. - Sorry. In fact my code is:

Function SelectOneListItem(selListBox As Variant)
If selListBox.ListIndex = -1 Then
    If selListBox.ListCount = 1 Then
        selListBox.SetFocus
        selListBox.Selected(0) = True
    End If
End If
End Function

However, I still get : "Object deosn't support this property or method".

Rockiroads:

I have used your selListBox.value = selListBox.itemdata(0) and it works. Seems obvious, now you've said it. Thanks.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84
ID: 24353241
rocki's method will work, of course.
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24353562
With your current code, it would probably work if defined as a listbox

Function SelectOneListItem(selListBox As ListBox)
If selListBox.ListIndex = -1 Then
    If selListBox.ListCount = 1 Then
        selListBox.SetFocus
        selListBox.Selected(0) = True
    End If
End If
End Function
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 24353579
One thing I forgot to add, and the reason why I prefer to use itemdata, is that I dont believe selected sets the listbox value, it just merely highlights an entry. Use of itemdata in setting the value sets both the value and selects item
0
 
LVL 84
ID: 24353692
Yes, the Selected property simply selects that item. Of course, at that point the Listbox would return whatever value is in the bound column for that listbox. IOW, if I select item 2, the listbox would be "set" to whatever value is in the ItemData for Item 2.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now