tags:

views:

72

answers:

5

I start out with text103.text having a text value of what I want to check C:\test.txt for. So if whatever's in text103.text matches up with whatever's in C:\test.txt label3.caption should read "success" yet every time I run it, I get "failure" why??

So here's the code in my button:

Private Sub Command1_Click()

    nFileNum = FreeFile
    Open "C:\test.txt" For Input As nFileNum
    lLineCount = 1

    Do While Not EOF(nFileNum)
       Line Input #nFileNum, sNextLine
       sNextLine = sNextLine & vbCrLf
       sText = sText & sNextLine
    Loop
    Text102.Text = sText
    Close nFileNum


    If Text102.Text = Text103.Text Then
        Label3.Caption = "success"
    Else
        Label3.Caption = "failure"
    End If


End Sub

Even when my text103.text starts out as "hello" and I edit the C:\test.txt to just say "hello" it always gives me label3.caption "failure"!!! Why???

+2  A: 

It's because you are adding newline characters text103.text does not have this.

Radu
Text103 is only one line and should always be only one line... Do I just remove Line Input #nFileNum, sNextLine?
Exist
Radu
+5  A: 

Possibly because you are always adding a newline to the data read from the file.
Does Text103.Text contain a new line too?

Update:

vbCrLf aka \r\n are part of the set of whitespace characters so you may not be able to see them directly.

Before If Text102.Text = Text103.Text Then try
msgbox "Len 102 " & Len(Text102.Text) & " Len 103 " & Len(Text103.Text)
this will show that the strings are different lengths, therefore they cannot be equal.

Alternatively, in immediate mode try ? "[" & text102.Text & "]" and ? "[" & text103.Text & "]" Assuming the word in question is "Hello", I'll bet the first will print

[Hello
]

and the second
[Hello]

Binary Worrier
Both text102.text and text103.text should only contain one line (a word).
Exist
Lets assume the text file contains `Hello`. In the loop you add a `vbCrlF` to it, so `Text102.Text` now contains `Hello` **plus two extra characters**, which are a carrige return and line feed (i.e. vbCrLf)
Binary Worrier
Yeah, but you are appending the vbCrLf to sNextLine. That means that sText includes one vbCrLf. Text103.txt doesn't appear to. Thus, they will never match. Perhaps you should show us where Text103.txt gets its value from.
Henry
+2  A: 

Could it be to do with your trailing carriage return? It looks like your file read will always have a vbCrLf on the end of it whereas possibly your text103 doesn't. Can you go into debug mode and confirm exactly what each string contains?

Chris
It doesn't give me any debugging errors.. it runs smooth, but the label caption always says failure.
Exist
Thank you, the vbCrLf seemed to be the culprit
Exist
+2  A: 

I'd make a guess that it's to do with the newlines (vbCrLf) that you add, or some similar character.

Otherwise it might be case dependant, you could try adding Option Compare Text at the top of the file.

ho1
Yes thank you, I simply removed "+ vbCrLf" and got success. Thak you ho1!
Exist
A: 

Try this:

Private Sub Command1_Click()

nFileNum = FreeFile
Open "C:\test.txt" For Input As nFileNum
lLineCount = 1

Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
   sNextLine = sNextLine & vbCrLf
   sText = sText & sNextLine
Loop
Text102.Text = sText
Close nFileNum


If Replace$(Text102.Text, VBCrLf, "") = Replace$(Text103.Text, VbCrLf, "") Then
    Label3.Caption = "success"
Else
    Label3.Caption = "failure"
End If


End Sub
pm_2
That would work but he shouldn't be adding new lines anyway, or reading the file like that :P
Radu