Solved

ListIndex error 7777 You've used the ListIndex property incorrectly

Posted on 2009-05-11
8
4,344 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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
 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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 …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

809 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