PDA

Click to See Complete Forum and Search --> : [info] When are '=' not equal?


Ecniv
Dec 22nd, 2005, 03:48 AM
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.

'---- 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.


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


:thumb:


In addition - if you open Access with no mdb in it, press ctrl+G to get to the immediates window and type in

?"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!

Webtest
Dec 23rd, 2005, 09:41 AM
I would have guessed that the binary compare would do what you want. Try using the explicit value "0" rather than the constant vbBinaryCompare:'From the 2003 HelpHeap:
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1.