none
Replace with like RRS feed

  • Question

  • How can I replace StrComp with the like function 

    ***************************************************Code************************************************

    StrComp(Me.SASP_Advisor_Search_Bar, [SASP Primary Advisor Name], vbTextCompare) = 0

    Friday, January 5, 2018 4:22 AM

Answers

All replies

  • StrComp(Me.SASP_Advisor_Search_Bar, [SASP Primary Advisor Name], vbTextCompare) = 0

    Hi SpeedKing,

    STrComp takes two parameters of type String.

    The value of    Me.SASP_Advisor_Search_Bar    is probably of type String, but the second parameter not. If you want to compare with the verbatim value, then you should enclose it with quotes, as in:

    StrComp(Me.SASP_Advisor_Search_Bar, "SASP Primary Advisor Name", vbTextCompare) = 0

    or use a variable:

    StrComp(Me.SASP_Advisor_Search_Bar, advisor_name, vbTextCompare) = 0

    Imb.


    Friday, January 5, 2018 6:58 AM
  • I don't know in what context this statement is used (VBA? SQL?) but the Like function is relatively straightforward:

    Dim CompareResult As Boolean

    CompareResult = Me.SASP_Advisor_Search_Bar Like [SASP Primary Advisor Name]

    You can find examples at the below link that use pattern matching characters:

    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, January 5, 2018 1:45 PM
  • Hi SpeedKing1000,

    Pardon me but to me:

    StrComp(string1, string2, vbTextCompare) = 0

    is equivalent to:

    string1 = string2

    So, if you're using the above in an If/Then check, you can replace it with the following:

    If string1 = string2 Then

    To me, Like is more equivalent to the InStr() function. For example, the following expression:

    If InStr(string1, string2) > 0 Then

    is similar to the following:

    If string2 Like "*" & string1 & "*" Then

    So, I guess what I am saying is you wouldn't really replace StrComp with Like.

    Just my 2 cents...

    Friday, January 5, 2018 6:45 PM
  • Okay, thank you.
    Tuesday, January 9, 2018 12:16 AM
  • That is another great way I will give it a try.
    Tuesday, January 9, 2018 12:16 AM
  • So I am using it in a if statement so I should be able to use InStr?
    Tuesday, January 9, 2018 12:18 AM