matching case

B

Bill H.

How do I compare the contents of a user field on a form to a string of
characters such that the two strings must match exactly, including case?

IE. User enters "test" but the comparison must match "TesT"

Thanks.
 
J

John Spencer (MVP)

Use the StrComp function.

strComp("test","TesT",0) will return Zero if the items match and 1 or -1 if
they don't match.

Check out the VBA on line help for a full explanation.
 
D

dan artuso

Hi,
I've always used my own function for this.
Put this in a standard module:

Public Function CaseCompare(str1 As String, str2 As String) As Boolean
On Error GoTo comp_err
Dim i As Integer
If Len(str1) <> Len(str2) Then
CaseCompare = False
Exit Function
Else
For i = 0 To Len(str1) - 1
If Asc(Mid(str1, i + 1, 1)) <> Asc(Mid(str2, i + 1, 1)) Then
CaseCompare = False
Exit Function
End If
Next
End If
CaseCompare = True

Exit Function
comp_err:
MsgBox Err.Description
CaseCompare = False

End Function

Now from code like this, you can test:
If CaseCompare(Me.yourField,"yourString") Then
'they are the same
Else
'they're not
End If
 
Top