• checking a name through a list of names (vbscript)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » checking a name through a list of names (vbscript)

    Author
    Topic
    #443940

    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

    Viewing 0 reply threads
    Author
    Replies
    • #1072229

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

      • #1072298

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

        • #1072308

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

          First, a simplistic solution:

          If InStr(strList, txtFrom) > 0 Then
          ' name occurs in list
          Else
          ' name does not occur in list
          End If

          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

          "Paul,John,Bob,Barry,Mark"

          and NOT

          "Paul, John, Bob,Barry,Mark"

          You can now use this code:

          strList = "," & strList & ","
          txtFrom = "," & txtFrom & ","
          If InStr(strList, txtFrom) > 0 Then
          ' name occurs in list
          Else
          ' name does not occur in list
          End If

          The commas before and after the variables make txtFrom look like this:

          ",Mark,"

          and strList like this:

          ",Paul,John,Bob,Barry,Mark,"

          InStr will only return a value > 0 if the entire name occurs in the list.

    Viewing 0 reply threads
    Reply To: checking a name through a list of names (vbscript)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: