CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Jul 2002
    Posts
    43

    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?

  2. #2
    Join Date
    Apr 1999
    Location
    Toronto, Canada
    Posts
    91
    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

  3. #3
    Join Date
    Jul 2002
    Posts
    43
    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?

  4. #4
    Join Date
    Jul 2002
    Location
    India
    Posts
    50
    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

  5. #5
    Join Date
    Jul 2002
    Posts
    43
    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.

  6. #6
    Join Date
    Jun 2002
    Location
    Lyman ME - USA | Oneonta NY - USA
    Posts
    399
    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!

  7. #7
    Join Date
    Jul 2002
    Posts
    43
    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?

  8. #8
    Join Date
    Jun 2002
    Location
    Lyman ME - USA | Oneonta NY - USA
    Posts
    399
    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!

  9. #9
    Join Date
    Jul 2002
    Posts
    43
    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?

  10. #10
    Join Date
    Aug 2008
    Posts
    1

    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
  •  





Click Here to Expand Forum to Full Width

Featured