Results 1 to 4 of 4

Thread: [RESOLVED] why does InStr miss?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Resolved [RESOLVED] why does InStr miss?

    when looking for a single char, instr seems to miss the occurance if the char occurs as the first or last position in the string.

    i have a work around, just repeat the first and last terms in the string, or insert a pad space.

    But why is this nessesary?


    VB Code:
    1. Public Function InStrW(ByRef LookIn As String, ByRef LookFor As String) As Boolean   ' break up lookfor into words, compares each
    2.  
    3. Dim arWS() As String
    4. Dim xx As Long
    5. Dim match As Boolean
    6. match = True
    7.  
    8. arWS = Split(Trim(LookFor), " ")
    9.  
    10. For xx = 0 To UBound(arWS) - 2
    11.         If InStr(LookIn, Trim(arWS(xx))) = 0 Then
    12.                 match = False
    13.         Else
    14.             Debug.Print "InstrW = True", (arWS(xx)) & "<", Str(xx), arWS(xx) & "<", "instr return: " & Str(InStr(LookIn, Trim(arWS(xx)))), LookIn, vbTab, LookFor
    15.             Debug.Print Trim(arWS(xx))
    16.            
    17.                 match = True
    18.                  InStrW = True
    19.                 xx = UBound(arWS) - 2
    20.                 Exit For
    21.         End If  'term in string?
    22. Next xx
    23. InStrW = match
    24.  
    25. End Function

    (i am subtracting 2 due to the pad space i had to use)

  2. #2
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: why does InStr miss?

    Hrm? I don't see any such problem with InStr:
    VB Code:
    1. Dim str As String
    2.  
    3.   str = "ABCDE"
    4.   Debug.Print "A:"; InStr(str, "A")
    5.   Debug.Print "C:"; InStr(str, "C")
    6.   Debug.Print "E:"; InStr(str, "E")
    Output:
    Code:
    A: 1 
    C: 3 
    E: 5
    What is it that your are missing? Can you show the code that doesn't give the result you expect?

  3. #3
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: why does InStr miss?

    Quote Originally Posted by rnd me
    when looking for a single char, instr seems to miss the occurance if the char occurs as the first or last position in the string.
    That's not happening to me, example..
    VB Code:
    1. MsgBox InStr("testing", "t") 'result: 1
    2.     MsgBox InStr("testing", "g") 'result: 7
    3.     '(InStrRev starts from the end)
    4.     MsgBox InStrRev("testing", "t") 'result: 4

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: why does InStr miss?

    if (two posts in a row) then call MyError


    If InStr(drvList, Left$(LB1.List(xx), 1)) Then LB1.Selected(xx) = True

    changed to :
    If InStr(1, drvList, Left$(LB1.List(xx), 1), vbTextCompare) Then LB1.Selected(xx) = True

    and it works ok. i figured since it was ok for you guys something i was doing was wrong. a little digging on msdn, and i found out that without specifying a compare type, binary is default. binary forces a case-sensitive match, and the string case was the problem.

    thank you two for posting! i wasn't sure if i was crazy...

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