Solved

Find if itemdata value in listbox matches current variable

Posted on 2010-08-12
3
244 Views
Last Modified: 2012-05-10
I have a piece of code which is pulling a value from an SQL table and assigning it to a variable called MVID.  I also have a listbox called lstAxis which has multiple select set on.  The 1st item in the listbox is "ALL" and after that it is populated with data which has a numeric value for each item's .itemdata.

What I need to do is run some code if the MVID variable matches EXACTLY a seleted item's .itemdata value or the ALL item.

I have looked at instr but that seems to throw up false negatives for instance if MVID = 2 then the code runs if the item's .itemdata is 2 or 222 or 123 etc.

Thanks in advance for your help  
0
Comment
Question by:simonwait
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33422984
Try this:
 
 If MVID = your item's .ItemData Then
 
0
 
LVL 1

Author Comment

by:simonwait
ID: 33423030
But I need to check multiple items in the list as there could be several selected.  Is there a way of looping?
0
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33423378
Try this:

    Dim i As Integer
    For i = 0 To List1.ListCount - 1
        If List1.Selected(i) Then
            If List1.ItemData(i) = MVID Then
                'match found
            End If
        End If
    Next

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question