Link to home
Start Free TrialLog in
Avatar of normenclature
normenclature

asked on

ListIndex property problem - works fine except for Access 2003

i have the following code that cycles through a listbox based on a letter someone types in a textbox.  It's part of the change event of the textbox.  It works beautifully on Access 2000, 2002, but NOT on 2003.  Does anyone have a clue as to why?  500 points for urgency.

here's the code:

Private Sub txtGo2_Change()
Dim s As String, p As String
Dim i As Integer, prevI As Integer
If txtGo2.Text = "" Then  'nothing typed
    Exit Sub
End If
s = Me.txtGo2.Text

'Note the use of the ListIndex property of the ListBox
'If the ListIndex is -1 means nothing selected
'If 0 means the first item selected
Me.lstProperty.SetFocus
Me.lstProperty.ListIndex = 0

For i = 0 To lstProperty.ListCount - 1
If i = 1000 Then Exit Sub
    'use the LIKE operator to compare
    'convert both to Uppercase as well so case does not matter
    p = lstProperty.Column(1, lstProperty.ListIndex)
    If UCase(p) Like UCase(s & "*") Then
        On Error Resume Next
       
        Forms("frmMain").Controls("lstProperty").ListIndex = i
        Me.txtGo2.SetFocus
        Me.txtGo2.SetFocus
        Me.txtGo2.SelStart = Len(Me.txtGo2.Text)
        'SendKeys "+{F2}"
        SetGlobalEye (i)
        Exit Sub
    End If
    'On Error GoTo Err_Controller
    lstProperty.ListIndex = lstProperty.ListIndex + 1
Next

Err_Exit:
    Exit Sub
Err_Controller:
      msgbox "There was an error"
        Exit Sub

End Sub
Avatar of normenclature
normenclature

ASKER

P.S.  I get an error 7777- "You've used the ListIndex Property incorrectly"
SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It looks like you have a few other places that could use the same correction.  Use the ListIndex property to retrieve the selected row, and the Selected property to set the selected row.

Private Sub txtGo2_Change()
Dim s As String, p As String
Dim i As Integer, prevI As Integer
If txtGo2.Text = "" Then  'nothing typed
    Exit Sub
End If
s = Me.txtGo2.Text

'Note the use of the ListIndex property of the ListBox
'If the ListIndex is -1 means nothing selected
'If 0 means the first item selected
Me.lstProperty.SetFocus
Me.lstProperty.Selected(0) = True                              '*** Here

For i = 0 To lstProperty.ListCount - 1
If i = 1000 Then Exit Sub
    'use the LIKE operator to compare
    'convert both to Uppercase as well so case does not matter
    p = lstProperty.Column(1, lstProperty.ListIndex)
    If UCase(p) Like UCase(s & "*") Then
        On Error Resume Next
       
        Forms("frmMain").Controls("lstProperty").Selected(i) = true          '****** Here
        Me.txtGo2.SetFocus
        Me.txtGo2.SetFocus
        Me.txtGo2.SelStart = Len(Me.txtGo2.Text)
        'SendKeys "+{F2}"
        SetGlobalEye (i)
        Exit Sub
    End If
    'On Error GoTo Err_Controller
    lstProperty.Selected ( lstProperty.ListIndex + 1) = True                   '**** here
Next

Err_Exit:
    Exit Sub
Err_Controller:
      msgbox "There was an error"
        Exit Sub

End Sub
Without going into a disection of the code I believe it is this line that is producing the error...

        lstProperty.ListIndex = lstProperty.ListIndex + 1

I'm betting the error occures when the value you attempt to set the .ListIndex property to exceeds the maximum value possible (.ListCount -1)

If you add the following code you'll avoid the error you're getting (I think) although as some have mentioned there are other issues apparent in the code worthy of review.

    If Me.lstProperty.ListIndex > (Me.lstProperty.ListCount - 1) Then   'Don't set the list index greater than the number of items avail...
        lstProperty.ListIndex = (lstProperty.ListIndex + 1)
    End If

Rick

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to all!   I will check these today and finalize this tonight.  I'm sure one of these will work.  Will advise soon--

Norm
Hi all--

mbizup:  your code statement seems to work, but then when it tries to set the focus on the txtGo2 textbox again, it "blows up" on me, in that Access closes and it offers to create a backup.

Any ideas?

n
Rick,

Thanks pal!  your code worked great!!  What is interesting is that your code works even in Access 2003 with the .ListIndex setting the long integer, but your code also works with mbizup's suggestion of using .Selected(i) = True , so Assisted points for mbizup

Thanks again!
Glad you liked it. :)

Rick