listindex error.

  • Thread starter Thread starter steve a
  • Start date Start date
S

steve a

Hi guys
I'm trying to get this list to automatically select the contents, however
the row source might present with nothing.
i've tried using:

Me.lstcost.SetFocus
If Me.lstcost > "" Then
Me.lstcost.ListIndex = 0
Me.lstcost = Me.[cost]

Else

End If

however if there is no data i get an error saying list index is not being
used properly..

for the row source i'm using:

lstcost.RowSource = "Select tblAll2.cost " & _
"FROM tblAll2 " & _
"WHERE tblAll2.name = '" & txtname.Value & "' " & _
"ORDER BY tblAll2.cost;"

however sometimes the name will not match, and therefore present the list
with nothing..
any ideas? i'm kinda stuck!

Thanks
Steve
 
You can't select data if there's no data to select! Check the ListCount
property to see whether there's data, and only set ListIndex if there is.

Also,

If Me.lstcost > "" Then

isn't a great idea. If nothing's selected, the list box will return Null,
not a zero-length string. Try the following instead:

If Len(Me.lstcost & vbNullString) > 0 Then

(Of course, I don't understand why you're trying to reset the list box if
something's already been selected!)
 
Hi doug.
Thanks for the info...
You've made me think about things, and how they are working.. and in the end
i've decided to limit the selection, so there will be no null entries..
suppose that solves the problem...
Many thanks for your time and knowledge.
steve

--
steve adlam


Douglas J. Steele said:
You can't select data if there's no data to select! Check the ListCount
property to see whether there's data, and only set ListIndex if there is.

Also,

If Me.lstcost > "" Then

isn't a great idea. If nothing's selected, the list box will return Null,
not a zero-length string. Try the following instead:

If Len(Me.lstcost & vbNullString) > 0 Then

(Of course, I don't understand why you're trying to reset the list box if
something's already been selected!)

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


steve a said:
Hi guys
I'm trying to get this list to automatically select the contents, however
the row source might present with nothing.
i've tried using:

Me.lstcost.SetFocus
If Me.lstcost > "" Then
Me.lstcost.ListIndex = 0
Me.lstcost = Me.[cost]

Else

End If

however if there is no data i get an error saying list index is not being
used properly..

for the row source i'm using:

lstcost.RowSource = "Select tblAll2.cost " & _
"FROM tblAll2 " & _
"WHERE tblAll2.name = '" & txtname.Value & "' " & _
"ORDER BY tblAll2.cost;"

however sometimes the name will not match, and therefore present the list
with nothing..
any ideas? i'm kinda stuck!

Thanks
Steve
 
Back
Top