borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (Exporting vb and access to msword) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : How do I find items in a VBA Listbox control
Poster Message
BlackDuck603
Level: Graduate


Registered: 04-10-2007
Posts: 13

icon How do I find items in a VBA Listbox control

I am using Access 2000. I am working on Visual Basic code behind my Access forms. I am fairly new to VBA....

I am trying to figure out a good way to check to see if an item is in a listbox control. In regular Visual Basic, the listbox control uses an Items object which has a Contains() method that is very easy to use to check if an item is in the listbox.
Here is an example (in Visual Basic) of what I am trying to do:

        Dim x As Integer
        Dim testValue As String = "Test"

        If Me.ListBox1.Items.Contains(testValue) Then
            ' There is at least one in the listbox
            ' Now go find them
            For x = 0 To Me.ListBox1.Items.Count - 1
                If Me.ListBox1.Items.Item(x) = testValue Then
                    MessageBox.Show("Found it at Index: " & x)
                End If
            Next x

        End If


The VBA listbox object does not seem to support this.

I imagine that I am not the first person to try to find an item in a listbox and was wondering if anyone has any recommendations on how best to go about this.

Thanks
Bill

19-11-2007 at 02:18 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 937
icon Re: How do I find items in a VBA Listbox control

Hi Bill,
Skip the contains test and continue with the rest. It's the only way, I'm afraid.


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

03-01-2008 at 12:12 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : How do I find items in a VBA Listbox control
Next Topic (Exporting vb and access to msword) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder