UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Listbox Error 7777 (listindex), Access 2010    
 
   
Teatime
post Jul 10 2012, 06:12 AM
Post#1



Posts: 91
Joined: 1-May 11
From: Germany


Okay, by now I'm slightly annoyed.
I'm trying to select a row in a listbox and position that selection in the middle of the listbox window. (By default it's positioned on the bottom of the window)
After some searching I came across this code by Stephen Lebans which basically does what I want to achieve.
So I adapted the code of his "method 1" to my form. Seemed to work - so far so good.
But at some point I started to run into an error 7777 ("You've used the ListIndex property incorrectly.")
Thinking I might have some incomplete knowledge of the ListIndex property I started modifying the code.
Worked, then at a later point again this error. More reading (not always helpful ... apparently that property was once read-only, but later changed to read/write), more modification.
The day before yesterday the code in it's current incarnation (see below) worked perfectly, only to give the bloody error again the following day, without any changes on the code (or any other code in the project for that matter).
My Code (based on Lebans code):
CODE
Public Sub ScrollListbox(ctl As ListBox, intVisibleRows As Integer, Optional boolNext As Boolean)
' http://bytes.com/topic/access/answers/191145-listbox-scroll-selected-item-code
' Always make NumRows an odd number if you want selected Row to be in the middle of the ListBox.
' listindex is 0-based
    Dim intActiveRow As Integer
    Dim lgListCount As Long
    
    lgListCount = IIf(ctl.ColumnHeads, ctl.ListCount - 1, ctl.ListCount)
    
    If lgListCount = 0 Then
        Exit Sub
    End If
    
    If boolNext And lgListCount - 1 > ctl.ListIndex Then
        intActiveRow = ctl.ListIndex + 1
    Else
        intActiveRow = ctl.ListIndex
    End If
    ' ListBox must have the Focus
    ctl.SetFocus
    ' Force ListBox to start from the top
    ctl.ListIndex = 0
    ' Force the Scroll offset we desire
    If lgListCount - 1 < intActiveRow Then
        ctl.ListIndex = ctl.ListCount - 1
    ElseIf lgListCount - 1 < Int(intActiveRow + (intVisibleRows / 2)) Then
       ctl.ListIndex = lgListCount - 1
    Else
       ctl.ListIndex = Int(intActiveRow + (intVisibleRows / 2))
    End If
    ' Now select the row without further scrolling
    'ctl.ListIndex = intActiveRow <- disabled earlier
    If ctl.ColumnHeads = True Then
        ctl = ctl.ItemData(intActiveRow + 1)
    Else
        ctl = ctl.ItemData(intActiveRow)
    End If
End Sub

So by now I suspect that this might have nothing to do with the code and the ListIndex property itself, but that something else might be going wrong and Access simply takes the next best error to handle the problem.
I've seen similar problems in the past, where a "defective" reference (not broken, but somehow not working correctly) caused completely unrelated error messages which would disappear after recreating the references.
That seems not to be the solution here since I've done that and the error prevails. I have compacted/repaired the db, decompiled it, but I have not yet transferred the code to a new database (which would be the next step)
In summary:
- Are there specific conditions which might cause the ListIndex property to fail? (Besides the listbox having focus)
- Could other problems result in an error 7777 which might not have anything to do with the ListIndex?
- Alternatives?
TIA
Thomas
Go to the top of the page
 
MikeLyons
post Jul 10 2012, 10:52 AM
Post#2


UtterAccess VIP
Posts: 1,933
Joined: 12-April 06
From: BC, Canada


Never tried to do what you are attempting so this is just a guess, but...
Is the ListBox control set to be MultiSelect? If so, then I don't believe the ListIndex property is going to work because it normally points to the single selected item in a single-select ListBox but in a multiselect ListBox that would be irrelevant.
Just a thought.
Mike
Go to the top of the page
 
Teatime
post Jul 10 2012, 05:20 PM
Post#3



Posts: 91
Joined: 1-May 11
From: Germany


Nope, plain single-select-listbox ;-)
Go to the top of the page
 
MikeLyons
post Jul 10 2012, 05:39 PM
Post#4


UtterAccess VIP
Posts: 1,933
Joined: 12-April 06
From: BC, Canada


Well in Access 2010 I created a test form with a listbox and tried out your function and didn't get any error.
Can you show an example of how you are actually invoking this code?
Mike
Go to the top of the page
 
Teatime
post Jul 13 2012, 05:39 PM
Post#5



Posts: 91
Joined: 1-May 11
From: Germany


I think I found the problem:
Setting the ListIndex triggers the Click-Event of the Listbox (which I would have never expected and makes no sense whatsoever to me).
The code I have attached to the Click-Event moves the focus to a CommandButton.
o when the procedure tries to set the ListIndex the second time (first to 0, then to the new value) the listbox no longer has the focus this action requires.
And now that I think of it it might be possible that I made changes regarding that SetFocus on the Click-Event, which would explain the sudden appearance of the error.
Mike, sorry for wasting your time on a matter I could have solved myself with more thorough examination :/
Will try to do better next time ...
Thanks
Thomas
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    20th November 2018 - 10:47 PM