Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Oct 2005
    Location
    Calgary, Alberta
    Posts
    205
    Thanks
    0
    Thanked 1 Time in 1 Post

    checking a name through a list of names (vbscript)

    Hi Lounge,

    Wondering if anyone knows a way I can write code which will take a variable called txtFrom and loop it through a list

    for example

    txtFrom = "Mark"

    If txtFrom is equal or matches any value in this list "Paul, John, Bob,Barry,Mark"
    then Control.Frame.Enable = True
    Else
    msgbox = "You cannot access this control"
    End if

    Anyone with a suggestion on how I can loop through a list looking fo a value?

    Thanks ahead

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: checking a name through a list of names (vbscript)

    Is the list stored in a string variable, or in an array, or in a range of cells?

  3. #3
    2 Star Lounger
    Join Date
    Oct 2005
    Location
    Calgary, Alberta
    Posts
    205
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: checking a name through a list of names (vbscript)

    the list would be a string varible hard coded within the sub routine

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: checking a name through a list of names (vbscript)

    Let's say that the list is stored in the variable strList.

    First, a simplistic solution:
    <code>
    If InStr(strList, txtFrom) > 0 Then
    ' name occurs in list
    Else
    ' name does not occur in list
    End If
    </code>
    But if your list contains a name such as Mark Anthony, or Marko, the code will still decide that "Mark" is part of the list. For more accuracy, make sure that there are no spaces after the comma in the list, so
    <code>
    "Paul,John,Bob,Barry,Mark"
    </code>
    and NOT
    <code>
    "Paul, John, Bob,Barry,Mark"
    </code>
    You can now use this code:
    <code>
    strList = "," & strList & ","
    txtFrom = "," & txtFrom & ","
    If InStr(strList, txtFrom) > 0 Then
    ' name occurs in list
    Else
    ' name does not occur in list
    End If
    </code>
    The commas before and after the variables make txtFrom look like this:
    <code>
    ",Mark,"
    </code>
    and strList like this:
    <code>
    ",Paul,John,Bob,Barry,Mark,"
    </code>
    InStr will only return a value > 0 if the entire name occurs in the list.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •