-
August 1st, 2002, 03:37 PM
#1
Setting ListIndex
I need to be able to make a listBox scroll to a specific record through. I think that the ListIndex property allows you to do this. According to to online MSDN help (msdn.microsoft.com), ListIndex is a read-only in both Office 2000 and Office XP; however in Office XP, it says that ListIndex can be set using this syntax:
Forms(formname).Controls(controlname).ListIndex = index
(see http://msdn.microsoft.com/library/de...olistindex.asp for the help article)
Nothing is mentioned about setting ListIndex in Office 2000. I've tried it using the syntax Forms.formname.controlname.ListIndex = index, and it doesn't work. The above syntax for Office XP doesn't work either.
I've read some threads on here where people have said you can assign ListIndex. How do you do this? What am I missing?
-
August 1st, 2002, 03:50 PM
#2
Hi. I'm not sure of the limitations of ListBox on different platforms, but for me, the next instructions work (to select fourth) element:
List1.ListIndex = 3
List1.Selected(3) = True
Maybe, someone else knows better
-
August 1st, 2002, 05:17 PM
#3
Thanks for the reply, but that doesn't work. I've tried that, and many variations on that syntax. Every time, it gives me this error: "Run-time Error '7777': You've used the ListIndex property incorrectly."
Maybe I'm going about this entirely wrong, but I've read many threads on this forum where setting ListIndex has been mentioned. Is there another way to scroll the listBox through code?
-
August 2nd, 2002, 04:13 AM
#4
I Want to know in which event you wrote that code,and please
write the code which is giving trouble .
i think list1.listindex =4 ,should work
-
August 2nd, 2002, 11:30 AM
#5
I have a Command button on my form called cmdSearch. I am writing the code for this in the click event for that button. Here's the code:
Code:
Private Sub cmdSearch_Click()
Dim criteria As String
Dim i As Integer
DoCmd.SetWarnings (False)
If (IsNull(Me.txtSearchBy)) Then
MsgBox "No Search Criteria Entered.", vbOKOnly + vbInformation
Exit Sub
End If
criteria = Me.txtSearchBy
DoCmd.RunSQL "SELECT Distinct tblAllFunds.Fund INTO tblSearchFunds FROM "
tblAllFunds, tblUserFunds WHERE ((tblUserFunds.Fund = tblAllFunds.Fund)
OR (tblUserFunds.FTYP = tblAllFunds.Ftype)) AND tblAllFunds.Title LIKE '" & criteria & "';"
If (DCount("[Fund]", "tblSearchFunds") = 0) Then
MsgBox "There are no funds that contain your search criteria.", vbOKOnly + vbInformation
Exit Sub
End If
DoCmd.OpenTable "tblSearchFunds"
For i = Forms.frmFunds.lstFund.ListIndex To Forms.frmFunds.lstFund.ListCount - 1
If (DCount("[Fund]", "tblSearchFunds", "[Fund] = '" & Forms.frmFunds.lstFund.ItemData(i) & "'") > 0) Then
Forms.frmFunds.lstFund.ListIndex = i 'This line crashes it
Exit Sub
End If
Next i
DoCmd.SetWarnings (True)
End Sub
The above code takes the text in txtSearchCriteria and constructs a table (tblSearchFunds) which contains a list of all the funds (which is what my ListBox is a list of) which match the criteria. It then goes through a for loop starting at the currently selected item (Forms.frmFunds.lstFund.ListIndex) and if it finds a match to one of the funds in tblSearchFunds, it should assign the ListIndex to this. However, no matter how I try to go about setting a value to ListIndex, it gives me this error: "Run-time Error '7777': You've used the ListIndex property incorrectly." I've tried it in other procedures, and tried creating a button that does nothing but set ListIndex to a given value. Like I said above, help in Access 2000 lists ListIndex as read-only. But so does that msdn page for Office XP - only it also shows a way to set the value of ListIndex.
My ListBox is set up as Multi select = extended. Thanks for any help you can provide.
-
August 2nd, 2002, 12:38 PM
#6
Originally posted by R2D2
... ListIndex as read-only.
That is because the list index is read only...you cannot set it. Try setting the itemdata? maybe that will work or maybe you could use indexes in your list and when you add one you use
for i = 1 to 10
list1.additem "Hi", i
next i
this will add 10 hi's each with an index and you could then use
j = list1.listindex
list1.remove list1.listindex
list1.additem "YAY", j
that would work around your problem to set specifics...i believe it should anyway
hope it helps
- nc
"In a world without walls and barriers, what need is there for windows and gates!" - a mac ad
"What was the best thing before sliced bread and when did sliced bread go out of existence?" - me
"Software is like sex, it's better when it's free." - Linus Torvalds <- gotten from Andreas Masur
Live Penguine! - Tux the linux mascot
Vivez le penguine!, ¡Viva en penguine!, Lang lebe der Pinguin!, Viva no penguine!, Viva sul penguine!
-
August 2nd, 2002, 01:05 PM
#7
Originally posted by booogt
That is because the list index is read only...you cannot set it.
I figured that, but I don't understand why this page (http://msdn.microsoft.com/library/de...olistindex.asp) would say "This property is read-only and isn't available in other views" and then also say "To set the ListIndex property value, you can use the following: Forms(formname).Controls(controlname).ListIndex = index." And I searched this forum for information on this topic, and over and over again, the replies were to set ListIndex to a value. That is where my confusion comes from.
Your idea is a good one, but I'm not having trouble selecting specific records; since it's a multiselectable listbox, I can use the selected(i) property to set whether or not a specific record is selected. The thing is that I'm making a search function, so I don't simply want to select all the records that contain the search criteria; I want to make the listBox scroll down to the next record that contains the search critieria, just like the "find" function built into many programs.
So...in a nutshell, I simply need to make my listbox scroll to a specific record through code. It's such a simple task that it seems like there must be some way to do this - Microsoft builds in so much into their development tools, how could they leave this out?
-
August 2nd, 2002, 01:28 PM
#8
ahh ok i thought you were trying to replace
hmmm, ok well to set the selected item try using list1.index = i
not listindex
maybe that will work
hope it helps
- nc
"In a world without walls and barriers, what need is there for windows and gates!" - a mac ad
"What was the best thing before sliced bread and when did sliced bread go out of existence?" - me
"Software is like sex, it's better when it's free." - Linus Torvalds <- gotten from Andreas Masur
Live Penguine! - Tux the linux mascot
Vivez le penguine!, ¡Viva en penguine!, Lang lebe der Pinguin!, Viva no penguine!, Viva sul penguine!
-
August 2nd, 2002, 01:57 PM
#9
Originally posted by booogt
hmmm, ok well to set the selected item try using list1.index = i
not listindex
Nope, index isn't a property of a list box - so it gives me a "Method or Data Member not found" error. Any other ideas?
-
August 16th, 2008, 05:44 PM
#10
Re: Setting ListIndex
The problem is that you cannot set the listindex of a listbox if the control doesn't have the focus
as it is explanned in msdn site, access 2003 reference, before you can use:
listbox.listindex = 2
to set the listindex to 2, you have to write this line of code:
listbox.setfocus
so the code would be
listbox.setfocus
listbox.listindex = 2
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|