Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare a string with "string"?

Status
Not open for further replies.

STPMB

Programmer
Sep 13, 2002
28
US
Is it possible to compare a string variable with a string in double quotes or do they both have to be defined string variables?
I know I can use the strComp function but am curious why the other won't work.
 
Try this in the immediate window

a$="abcd"
?strcomp("ABCD",a$)

It (correctly) produces -1 for me.

Look up StrComp function in VBHelp to check all the options Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
I have checked into using the string compare function but one of my colleagues is trying to do
Dim strVar1 as string
'The strVar1 is filled in the program and he wants to compare it with the value in quotes. If the strVar1 is the same length as the value in quotes shouldn't this type of comparison work? If not should the strVar1 variable have all its blanks removed before doing the comparison.

If strVar1 = "Var1" then
process
else
other process
end
 
Yes you will need to strip any leading or trailing blanks - look for the Trim() function Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 

Try this code, (Notice the "Tt" in test versus Test) and see what happens.

[tt]Option Explicit

Dim S As String

Private Sub Form_Load()

S = "test"

If S = "Test" Then
MsgBox "match"
ElseIf S < &quot;Test&quot; Then
MsgBox &quot;Variable less than&quot;
ElseIf S > &quot;Test&quot; Then
MsgBox &quot;Variable Greater Than&quot;
End If

End Sub
[/tt]

Then try this code (Notice &quot;Option Compare Text&quot;) and see what happens

[tt]Option Compare Text
Option Explicit

Dim S As String

Private Sub Form_Load()

S = &quot;test&quot;

If S = &quot;Test&quot; Then
MsgBox &quot;match&quot;
ElseIf S < &quot;Test&quot; Then
MsgBox &quot;Variable less than&quot;
ElseIf S > &quot;Test&quot; Then
MsgBox &quot;Variable Greater Than&quot;
End If

End Sub
[/tt]

I hope this helps
 
Thank you both for your help. Looks like my colleaque forgot about the trim function. I did too. Once he added the trim function the comparison worked fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top