Programming in VB

  Pusherman 21:56 27 Apr 04
Locked

Consider the following piece of code:

Dim x As String
Open "myfile.txt" For Input As #1
Input #1, x

The first line of myfile.txt contains the line 'the cat sat on the mat' so when the program is first run that is what the variable x equals. But if the first line is 'the cat, sat on the mat' (note the comma) then the variable x would equal 'the cat' and the rest of the line would be ignored. How do I get the variable x to equal 'the cat, sat on the mat'

Hope I've explained this clearly.
Thanks, Pusherman

  VoG II 21:59 27 Apr 04

Is this VB (the real thing) or Visual Basic for Applications (VBA)?

  Pusherman 22:01 27 Apr 04

VB6 the real thing.

  VoG II 22:02 27 Apr 04

That's me out of the discussion then. Sorry.

  Pusherman 22:03 27 Apr 04

And I've noticed that it doesn't look vary good on screen, hope this is better:

Dim x As String

Open "myfile.txt" For Input As #1

Input #1, x

  Pusherman 07:23 28 Apr 04

bump

  rogertjj 09:28 28 Apr 04

give this a try

On Error Resume Next


Dim x As String


Dim y As String


Open "C:\test.txt" For Input As #1


Input #1, x, y


x = x & " " & y


x = trim(x)


Close #1


This will work if there is only one comma in the line of text. You could add more variables if you wanted to.

  spargo 12:48 28 Apr 04

Try using 'Line Input' instead

fNum = FreeFile()
Open "Mytext.txt" For Input As #fNum
Line Input #fNum, l1
Line Input #fNum, l2
Line Input #fNum, l3
Close #fNum
Print l1
Print l2
Print l3

The file "Mytext.txt" was created in 'Notepad' and consisted of three lines as follows

the cat sat on the mat
the cat, sat on the mat.
the cat, sat, on the mat

VB uses a "," as a delimiter so can get confused.

Hope this helps.

  spargo 12:51 28 Apr 04

sorry format was spoilt, try again.


Try using 'Line Input' instead
fNum = FreeFile()

Open "Mytext.txt" For Input As #fNum

Line Input #fNum, l1

Line Input #fNum, l2

Line Input #fNum, l3

Close #fNum
Print l1

Print l2

Print l3

The file "Mytext.txt" was created in 'Notepad' and consisted of three lines as follows

the cat sat on the mat

the cat, sat on the mat.

the cat, sat, on the mat

VB uses a "," as a delimiter so can get confused.

Hope this helps.

  Pusherman 17:18 28 Apr 04

Thanks Spargo works a treat!

Pusherman.

This thread is now locked and can not be replied to.

Amazon Fire HD 8 review: A brilliant combination of function and value – with one massive caveat

1995-2015: How technology has changed the world in 20 years

How to create an introvert-friendly workplace

Apple Watch 2 review | Apple Watch Series 2 review: New Apple Watch is faster, brighter, water-resit…