Use of ListIndex

  • Thread starter Thread starter Asbjørn Søbye
  • Start date Start date
A

Asbjørn Søbye

I've resently switched to Access 2003 (from 2000). When I open a form where
I want two combo-boxes to be filled in with the two first values in the
respective tables, I get
an error message like this: "Run-time error '7777': You have used the
property ListIndex wrong" (poorly translated from norwegian ....). The
debugging highlightes the 5th line in the following procedure:

Private Sub Form_Open(Cancel As Integer)
Me.cbxMedlemsStatus.SetFocus
Me.cbxMedlemsStatus.ListIndex = 0
Me.cbxVelgMedlem.SetFocus
Me.cbxVelgMedlem.ListIndex = 0

' Finn postene som passer til kontrollen.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MedlemsID] = " & Str(Me![cbxVelgMedlem])
Me.Bookmark = rs.Bookmark
End Sub

When I remove the line the procedure runs, but then the combobox is empty
instead of showing the first value. What's wrong in this procedure? And why
doesn't the first ListIndex cause the same error?

I hope you can help me out, it's frustrating .....

Asbjørn Søbye
 
Asbjørn Søbye said:
I've resently switched to Access 2003 (from 2000). When I open a form
where I want two combo-boxes to be filled in with the two first
values in the respective tables, I get
an error message like this: "Run-time error '7777': You have used the
property ListIndex wrong" (poorly translated from norwegian ....). The
debugging highlightes the 5th line in the following procedure:

Private Sub Form_Open(Cancel As Integer)
Me.cbxMedlemsStatus.SetFocus
Me.cbxMedlemsStatus.ListIndex = 0
Me.cbxVelgMedlem.SetFocus
Me.cbxVelgMedlem.ListIndex = 0

' Finn postene som passer til kontrollen.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MedlemsID] = " & Str(Me![cbxVelgMedlem])
Me.Bookmark = rs.Bookmark
End Sub

When I remove the line the procedure runs, but then the combobox is
empty instead of showing the first value. What's wrong in this
procedure? And why doesn't the first ListIndex cause the same error?

I hope you can help me out, it's frustrating .....

My guess is that it's a timing issue, that the control hasn't been fully
instantiated yet, or its rowsource hasn't been loaded. Try moving all
the code from the Open event to the Load event. That makes sense
anyway, because the form's recordset hasn't normally been loaded yet in
the Open event anyway, though your reference to it will force it to load
early.
 
That didn't work ... :-( Same error

Dirk Goldgar said:
Asbjørn Søbye said:
I've resently switched to Access 2003 (from 2000). When I open a form
where I want two combo-boxes to be filled in with the two first
values in the respective tables, I get
an error message like this: "Run-time error '7777': You have used the
property ListIndex wrong" (poorly translated from norwegian ....). The
debugging highlightes the 5th line in the following procedure:

Private Sub Form_Open(Cancel As Integer)
Me.cbxMedlemsStatus.SetFocus
Me.cbxMedlemsStatus.ListIndex = 0
Me.cbxVelgMedlem.SetFocus
Me.cbxVelgMedlem.ListIndex = 0

' Finn postene som passer til kontrollen.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MedlemsID] = " & Str(Me![cbxVelgMedlem])
Me.Bookmark = rs.Bookmark
End Sub

When I remove the line the procedure runs, but then the combobox is
empty instead of showing the first value. What's wrong in this
procedure? And why doesn't the first ListIndex cause the same error?

I hope you can help me out, it's frustrating .....

My guess is that it's a timing issue, that the control hasn't been fully
instantiated yet, or its rowsource hasn't been loaded. Try moving all
the code from the Open event to the Load event. That makes sense
anyway, because the form's recordset hasn't normally been loaded yet in
the Open event anyway, though your reference to it will force it to load
early.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Asbjørn Søbye said:
That didn't work ... :-( Same error

Dirk Goldgar said:
Asbjørn Søbye said:
I've resently switched to Access 2003 (from 2000). When I open a
form where I want two combo-boxes to be filled in with the two first
values in the respective tables, I get
an error message like this: "Run-time error '7777': You have used
the property ListIndex wrong" (poorly translated from norwegian
....). The debugging highlightes the 5th line in the following
procedure:

Private Sub Form_Open(Cancel As Integer)
Me.cbxMedlemsStatus.SetFocus
Me.cbxMedlemsStatus.ListIndex = 0
Me.cbxVelgMedlem.SetFocus
Me.cbxVelgMedlem.ListIndex = 0

' Finn postene som passer til kontrollen.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MedlemsID] = " & Str(Me![cbxVelgMedlem])
Me.Bookmark = rs.Bookmark
End Sub

When I remove the line the procedure runs, but then the combobox is
empty instead of showing the first value. What's wrong in this
procedure? And why doesn't the first ListIndex cause the same error?

I hope you can help me out, it's frustrating .....

My guess is that it's a timing issue, that the control hasn't been
fully instantiated yet, or its rowsource hasn't been loaded. Try
moving all the code from the Open event to the Load event. That
makes sense anyway, because the form's recordset hasn't normally
been loaded yet in the Open event anyway, though your reference to
it will force it to load early.

Odd, I tried the identical code in a test form and it worked fine, even
in the Open event. Is there anything at all unusual about the
rowsources of the combo boxes?
 
I have seen this issue and it was always related to the fact that the
Control was not fully instantiated and therefore could not accept the
focus.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.


Dirk Goldgar said:
Asbjørn Søbye said:
That didn't work ... :-( Same error

Dirk Goldgar said:
I've resently switched to Access 2003 (from 2000). When I open a
form where I want two combo-boxes to be filled in with the two first
values in the respective tables, I get
an error message like this: "Run-time error '7777': You have used
the property ListIndex wrong" (poorly translated from norwegian
....). The debugging highlightes the 5th line in the following
procedure:

Private Sub Form_Open(Cancel As Integer)
Me.cbxMedlemsStatus.SetFocus
Me.cbxMedlemsStatus.ListIndex = 0
Me.cbxVelgMedlem.SetFocus
Me.cbxVelgMedlem.ListIndex = 0

' Finn postene som passer til kontrollen.
Dim rs As Object

Set rs = Me.Recordset.Clone
rs.FindFirst "[MedlemsID] = " & Str(Me![cbxVelgMedlem])
Me.Bookmark = rs.Bookmark
End Sub

When I remove the line the procedure runs, but then the combobox is
empty instead of showing the first value. What's wrong in this
procedure? And why doesn't the first ListIndex cause the same error?

I hope you can help me out, it's frustrating .....

My guess is that it's a timing issue, that the control hasn't been
fully instantiated yet, or its rowsource hasn't been loaded. Try
moving all the code from the Open event to the Load event. That
makes sense anyway, because the form's recordset hasn't normally
been loaded yet in the Open event anyway, though your reference to
it will force it to load early.

Odd, I tried the identical code in a test form and it worked fine, even
in the Open event. Is there anything at all unusual about the
rowsources of the combo boxes?

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
"Stephen Lebans"
I have seen this issue and it was always related to the fact that the
Control was not fully instantiated and therefore could not accept the
focus.

That's what I was thinking. But shouldn't shifting the code to the Load
event take care of that? Otherwise, how is one to know when the control
is fully instantiated?
 
When I have had it fail the code was already in the Load event. Perhaps
it has to do with the rowsource as you mentioned. It is definately a
timing related issue.
I could not nail down exactly the reason for intermittent failure so I
was able to code around the issue for my ToolTips class. Originally I
tried to SetFocus to the control in order to easily determine its hWnd.
The solution was to enumerate the Forms' individual control windows to
determine the ListBox's hWnd. That would not help in this case as the
user wants to select a specific row.

If I remember the original intent of the OP he just wants to preselect a
row in his Combo controls when the form loads. I would suggest he use
one of the standard approaches such as:
Me.Combo.Value = Me.Combo.ItemData(0)

Remember to allow for the first entry in the ItemData collection if you
have the Column Headings prop turned On.
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
"Stephen Lebans"
When I have had it fail the code was already in the Load event.
Perhaps it has to do with the rowsource as you mentioned. It is
definately a timing related issue.
I could not nail down exactly the reason for intermittent failure so I
was able to code around the issue for my ToolTips class. Originally I
tried to SetFocus to the control in order to easily determine its
hWnd. The solution was to enumerate the Forms' individual control
windows to determine the ListBox's hWnd. That would not help in this
case as the user wants to select a specific row.

If I remember the original intent of the OP he just wants to
preselect a row in his Combo controls when the form loads. I would
suggest he use one of the standard approaches such as:
Me.Combo.Value = Me.Combo.ItemData(0)

Remember to allow for the first entry in the ItemData collection if
you have the Column Headings prop turned On.

Right. This is pretty safe:

With Me.MyCombo
.Value = .ItemData(Abs(.ColumnHeads))
End With
 
Thanks Dirk,
I absolutely forget to use my head sometimes.<grin>
:-)
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Back
Top