Results 1 to 2 of 2

Thread: [info] When are '=' not equal?

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    [info] When are '=' not equal?

    Hi,

    Came across this recently in Access. Trying to make sure that the password entered on a form matches (exactly) the password held in the table.

    However, topcat would equal TopCaT and allow the process.

    I found a function in VBA called strComp. This has several comparisons, but they didn't work too well.
    Code:
    '---- try these in the immediates window
    ?"b"="B"
    ?StrComp("b","B",vbBinaryCompare)
    ?StrComp("b", "B", vbTextCompare )
    ?StrComp("bb", "bb", vbTextCompare )
    They didn't work for me (may be they do for you post up and let me know!)

    So I wrote the following and thought that it may come in handy for someone.

    Code:
    Public Function CompareText(ByVal strText1 As String, ByVal strText2 As String) As Boolean
        
    '---- Compares two texts together
    '----   You might think this is pointless BUT IT AIN'T!!
    
    '---- try the following in the immediates window
    '---- ?"b"="B"
    '---- ?StrComp("b","B",vbBinaryCompare)
    '---- ?StrComp("b", "B", vbTextCompare )
    '---- ?StrComp("bb", "bb", vbTextCompare )
    
        Dim lngLoop As Long
        
        On Error Resume Next
        
        CompareText = False
        
        If Len(strText1) <> Len(strText2) Then Exit Function
        
        CompareText = True
        
        For lngLoop = 1 To Len(strText1)
            If Asc(Mid(strText1, lngLoop, 1)) <> Asc(Mid(strText2, lngLoop, 1)) Then
                CompareText = False
                Exit Function
            End If
        Next
        
        If Err.Number <> 0 Then
            Debug.Print "Compare Text", Err.Number, Err.Description
        End If
        
    End Function


    [edit]
    In addition - if you open Access with no mdb in it, press ctrl+G to get to the immediates window and type in
    Code:
    ?"B"="b"
    The result is False (as it should be). Load in an mdb (or create a new blank one) and try again. It returns True. Nice!
    [/edit]
    Last edited by Ecniv; Dec 22nd, 2005 at 06:54 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: [info] When are '=' not equal?

    I would have guessed that the binary compare would do what you want. Try using the explicit value "0" rather than the constant vbBinaryCompare:
    Code:
    'From the 2003 HelpHeap:
    Dim MyStr1, MyStr2, MyComp
    MyStr1 = "ABCD": MyStr2 = "abcd"    ' Define variables.
    MyComp = StrComp(MyStr1, MyStr2, 0)    ' Returns -1.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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