StrComp

aziz rasul

Active member
Local time
Today, 08:30
Joined
Jun 26, 2000
Messages
1,935
Can anyone tell me why the following code doesn't work?

Option Compare Text
Dim string1 as Variant
string1 = StrComp("ABC", "abc", -1)


If I use string1 = StrComp("ABC", "abc", vbUseCompareOption)
then it's OK. However according to online help, vbUseCompareOption is the same as -1.
 
While the Help File states there are four Enum Options

vbUseCompareOption = -1
vbBinaryCompare = 0
vbTextCompare = 1
vbDatabaseCompare = 2

The Enumeration of the function only shows three of them (0 - 2)

As a test I printed out the value of vbUseCompareOption and low a behold it is empty. This will result in using the Optional Value of vbBinaryCompare. -1 seems to evoke the Error 5.

This means:

1. Microsoft Fixed this function.
2. The people writing the Help file where not kept in the loop.
 
I didn't understand what u meant by "Microsoft Fixed this function. "
 
What is ment is more Tongue-In-Cheek.

At one time this function was going to be able to do this, but now it's not.
 
While I'm on the subject of StrComp, online help states that if either string1 or string2 is Null, then StrComp returns a Null value. I tried

Option Compare Database

Dim var1 As Variant

var1 = StrComp("ABC", Null)

and I get -1 NOT Null as the online help suggests.
 
I used this code and I got Null for each case, so I'm not sure what the problem is, as the help file seems to be correct here.

Code:
Public Sub StrCompTest()

Dim x1 As Variant
Dim x2 As Variant
Dim x3 As Variant

x2 = "ABC": x3 = Null

    x1 = StrComp(x2, x3, vbBinaryCompare)
    Debug.Print x1
    x1 = StrComp(x2, x3, vbDatabaseCompare)
    Debug.Print x1
    x1 = StrComp(x2, x3, vbTextCompare)
    Debug.Print x1
    x1 = StrComp(x2, x3)
    Debug.Print x1
    x1 = StrComp("ABC", Null)
    Debug.Print x1


End Sub
 
I don't know what I was doing before. If I use the code you have given Travis, it now gives me a Null value as expected.

the code I was using also now gives me a Null value.

Thanks for the help.
 
The StrComp function has the following return values:


string1 is less than string2 -1
string1 is equal to string2 0
string1 is greater than string2 1
string1 or string2 is Null Null
 
One other way to just compare in code without using the strComp function is if you want to change this in your module:

Option Compare Database

to

Option Compare Binary

then you can use
Code:
Dim str1 As String
Dim str2 As String
 
str1 = "abc"
str2 = "ABC"
 
If str1 = str2 Then
   Debug.Print "Equal"
Else
   Debug.Print "Not Equal"
End If

And in that example it would return Not Equal.
 
Quite useful in a module used solely for checking case sensitive passwords.
 

Users who are viewing this thread

Back
Top Bottom