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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This is killing me.

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
0
0
GB
I have two stings, Temp1 and Temp2. If I comapre each whole string then I am told they are unequal.

If I cycle through and compare each character with its corresponding chracter in the other string I find that each characted is the same!

I used this:

In the following if statement, the second message box appears:

If temp1 = temp2 Then
MsgBox "Match"
Else
MsgBox "No Match"
End If

In the following loop the second message box appears for each character:

For i = 1 To Len(temp1)
If Mid(temp1, i, 1) <> Mid(temp2, i, 1) Then
MsgBox Mid(temp1, i,1) + &quot;<>&quot; + Mid(temp2, i, 1)
Else
MsgBox Mid(temp1, i,1) + &quot;=&quot; + Mid(temp2, i, 1)
End If
Next i

I have checked, and both strings are the same length. What on earth could possibly cause something like this to happen? As far as I know the two strings are the same!

 
Hi,
Does temp2 have a space at the end of it? Try:
For i = 1 to len(temp2) ...etc
 
You should use the Like comparison to compare strings. You may also want to change the case if the comparison is not case sensitive:
Code:
If UCase(temp1) Like UCase(temp2) Then
    'Match
Else
    'No Match
End If
 
Hmmmmmmmmmmmmmm,

elziko,

You're spoofing us? right?

Generally, the Case is NOT at issue when conmparing strings. Adding even one space (Chr(32)) to a string would make not equal to the original string. As 'they' say, ipso ergo - your are kidding.

Please include the two strings for the problem. (You know, the two which are un-equal as strings, but equal on the character by character basis).

I &quot;formalized&quot; the code snippet as a Function:

Code:
Public Function basChkStr(Str1 As String, Str2 As String) As Boolean

    Dim Idx As Integer

    basChkStr = True

    If Str1 = Str2 Then
        Debug.Print Str1 &amp; &quot; = &quot; &amp; Str2
        'MsgBox &quot;Match&quot;
     Else
        Debug.Print Str1 &amp; &quot; <> &quot; &amp; Str2
        basChkStr = False
        'MsgBox &quot;No Match&quot;
    End If

    'In the following loop the second message box appears for each character:

    For Idx = 1 To Len(Str1)
        If Mid(Str1, Idx, 1) <> Mid(Str2, Idx, 1) Then
            'MsgBox Mid(Str1, I, 1) + &quot;<>&quot; + Mid(Str2, I, 1)
            Debug.Print Idx, Mid(Str1, Idx, 1) &amp; &quot; <> &quot; &amp; Mid(Str2, Idx, 1)
            basChkStr = False
        Else
            Debug.Print Idx, Mid(Str1, Idx, 1) &amp; &quot; = &quot; &amp; Mid(Str2, Idx, 1)
            'MsgBox Mid(Str1, Idx, 1) + &quot;=&quot; + Mid(Str2, Idx, 1)
        End If
    Next Idx

End Function

and ran a test:

TheStr$ = &quot;The Quick brown Fox jumped over the lazy brown dog&quot;
? basChkStr(TheStr$, TheStr$)
The Quick brown Fox jumped over the lazy brown dog = The Quick brown Fox jumped over the lazy brown dog
1 T = T
2 h = h
3 e = e
4 =
5 Q = Q
6 u = u
7 i = i
8 c = c
9 k = k
10 =
11 b = b
12 r = r
13 o = o
14 w = w
15 n = n
16 =
17 F = F
18 o = o
19 x = x
20 =
21 j = j
22 u = u
23 m = m
24 p = p
25 e = e
26 d = d
27 =
28 o = o
29 v = v
30 e = e
31 r = r
32 =
33 t = t
34 h = h
35 e = e
36 =
37 l = l
38 a = a
39 z = z
40 y = y
41 =
42 b = b
43 r = r
44 o = o
45 w = w
46 n = n
47 =
48 d = d
49 o = o
50 g = g
True

A second test:
TheStrII$ = &quot;The Quick brown Fox jumped over the lazy brown dog&quot; &amp; &quot; &quot;
TheStr$ = &quot;The Quick brown Fox jumped over the lazy brown dog&quot;
? basChkStr(TheStr$, TheStrII$)
The Quick brown Fox jumped over the lazy brown dog <> The Quick brown Fox jumped over the lazy brown dog
1 T = T
2 h = h
3 e = e
4 =
5 Q = Q
6 u = u
7 i = i
8 c = c
9 k = k
10 =
11 b = b
12 r = r
13 o = o
14 w = w
15 n = n
16 =
17 F = F
18 o = o
19 x = x
20 =
21 j = j
22 u = u
23 m = m
24 p = p
25 e = e
26 d = d
27 =
28 o = o
29 v = v
30 e = e
31 r = r
32 =
33 t = t
34 h = h
35 e = e
36 =
37 l = l
38 a = a
39 z = z
40 y = y
41 =
42 b = b
43 r = r
44 o = o
45 w = w
46 n = n
47 =
48 d = d
49 o = o
50 g = g
False


And, A third test

TheStrII$ = &quot;The Quick brown fox jumped over the lazy brown Dog&quot;
TheStr$ = &quot;The Quick brown Fox jumped over the lazy brown dog&quot;
? basChkStr(TheStr$, TheStrII$)
The Quick brown Fox jumped over the lazy brown dog = The Quick brown fox jumped over the lazy brown Dog
1 T = T
2 h = h
3 e = e
4 =
5 Q = Q
6 u = u
7 i = i
8 c = c
9 k = k
10 =
11 b = b
12 r = r
13 o = o
14 w = w
15 n = n
16 =
17 F = f
18 o = o
19 x = x
20 =
21 j = j
22 u = u
23 m = m
24 p = p
25 e = e
26 d = d
27 =
28 o = o
29 v = v
30 e = e
31 r = r
32 =
33 t = t
34 h = h
35 e = e
36 =
37 l = l
38 a = a
39 z = z
40 y = y
41 =
42 b = b
43 r = r
44 o = o
45 w = w
46 n = n
47 =
48 d = D
49 o = o
50 g = g
True




MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
This is no spoof! I wish it was.

Each string has the same length, hence one does not have an extra space.

I cant just post the strings. Its a bit more complicated. I'm using a function found on a web site to check if a folder exists. It thinks a folder doesn't exist when it does. So I've been looking into it and it would seem that the problem is in comparing two strings which doesn't seem to work. The actual name of the folder I'm looking for is decided on run-time and compared with a folder name decided on at design time.

If I debug.print the folder name created run-time and then copy this into my code and compare the two strings (both created design-time) then they are found to be equal! So if I did just post the strings you will tell me it works, but they dont if one is created run-time!


This is such a mess. Why me? Why on friday?

Thanks very much for your help.

elziko
 
I've been looking into this all morning. I've come up with this:

When I concatenate two strings one string is often replaced by a number of question marks. This happens wether I use the &quot;+&quot; or the &quot;&amp;&quot; operator:

This code:

Dim InDatabasePath, InRecordStorName, InFileName, As String

InDatabasePath = StoragePhotosPath &amp; Safe_Folder_Name(Database.List(Database.ListIndex))
InRecordStorName = &quot;\&quot; &amp; Safe_Folder_Name(ConnectedField.Text)
InFileName = &quot;\&quot; &amp; File.Text &amp; &quot;.jpg&quot;

Debug.Print InDatabasePath
Debug.Print InRecordStorName
Debug.Print InFileName

Debug.Print InDatabasePath &amp; InRecordStorName &amp; InFileName

Gives this in the immediate window:

\CLEVELAND STREET
\Clear Day Bkgrd.jpg
c:\photos\storage\Inspections Database?????????? ?????? \Clear Day Bkgrd.jpg

NOTE: Safe_Folder_Name simply replaces any forbidden characters for a folder name with the fobbidden characters ascii number surrounded in exclamation marks:

so eg. &quot;\&quot; changes to &quot;!92!&quot;.

Any ideas, its probably something really simple. But then again its probably not.

Many thanks,

elziko
 

Check me on the below hack/smash of your code. It 'Looks Like' the First (single variable print) of [InDAtabasePath] does not produce any output?

Debug.Print InDatabasePath
Debug.Print InRecordStorName
Debug.Print InFileName

[red]Debug.Print InDatabasePath &amp; InRecordStorName &amp; InFileName[/red]

Gives this in the immediate window:

[green]BUT THIS DOESN'T PRINT[/green]
\CLEVELAND STREET
\Clear Day Bkgrd.jpg
[red]c:\photos\storage\Inspections Database?????????? ?????? \Clear Day Bkgrd.jpg[/red]

Also, look at the concatenation output. It appears that [InRecordStoreName] Has a non printing character (see the space between the &quot;??? ...?&quot; and &quot;\ Clear Day ...&quot;

I would (of course) apply my favorite process to this.

Dimension EVERTHING EXPLICITLY
Dim InDatabasePath as String
Dim InRecordStorName as String
Dim InFileName, As String

Print ALL of the variables AND their characteristics (IsNumeric, IsText, Len ... ) each time ANY variable is Assigned a value (Including the &quot;inputs&quot; - e.g. &quot;StoragePhotosPath&quot;)


REMOVE EVERTHING NOT ABSOLOUTLEY ESSIENTIAL. Start w/o the frills like &quot;Safe_Folder_Name&quot;

I KNOW this is tedious, but solving obscure problems via the 'Sherlock Holmes' approach usually works.

(He - Sherlock - is always telling Dr. Watson: Eliminate the posabilities untill there is only one left. That, no matter how improbable, must be the answeer.


MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
I have had problems in the past using &quot;=&quot; with strings when I knew that they were eqaul. Have you tried?

If Strcomp(temp1,temp2) = 0 Then
 
I notice that your first bit of comparison code is for equality, while the second is for inequality - and in both cases, the comparison comes out False. This is just the sort of thing that happens when one or both of the comparands is a Null value. But of course, if you've Dim'ed them As String, they couldn't be Null.

You did Dim them As String, didn't you? You know better than to let them be implicitly Dim'ed, don't you?
::Rick cocks one eyebrow suspiciously::
Rick Sprague
 
Yeah I always do everything explicitly, I finally found out that the problem was with two functions that I downloaded from a VB site. Stupidly I just assumed that they would work perfectly whereas they were actually very badly written, probably only working for the precise situation where the aurthor orginally used them.

Lesson learnt.

Thanks all,

elziko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top