Click to See Complete Forum and Search --> : File Reading


Lorna
July 16th, 2001, 04:33 AM
Me again
I receive a downloaded file from a mainframe env. that I process. How do I input this info if the file is comma delimited? How do I tell VB that the file is delimited? Data looks like this:
"i",1,"017925","2001/03/31","0380248.00"

Pse help!

thanks

Andrew_Fryer
July 16th, 2001, 04:35 AM
Use the INSTR command to determine the position of the commas in the string and split it that way.

Lorna
July 16th, 2001, 04:47 AM
ok thanks - I've looked up the format of the INSTR command, I'm not sure how to do it. If I find a comma in the input string - how do I handle it code wise? Obviously I'll have to have some sort of a counter that I can substract 1 from if the current char is a comma?

Andrew_Fryer
July 16th, 2001, 04:58 AM
The instr command will return the first position in which the comma is found. Therefore, if you have more than one comma then you will need to set the starting position for the search to be higher than the position of the previous comma, otherwise it will keep finding the same one, i.e



commaPos = 1

Do

previous = commaPos

' find the first - or next comma position

commaPos = instr(myText, start, ',')

' if it cannot be found then exit

if (commaPos = 0) then
exit do
end if

' DO SOMETHING HERE WITH THE DATA

' set the start position so that it looks for
' the next comma

start = commaPos + 1
Loop





In order to extract the chunk of text use the Mid command with the variables start and previous to give you the relavent positions.

Hope this helps

Andrew

amitrgholap
July 16th, 2001, 07:08 AM
Hi,
this is a v. simple eg. pl. modify it
dim arr() as string
arr = split("qwe,asd,zxc", ",")
arr(0) = qwe
arr(1) = asd
arr(2) = zxc
also ubound(arr) + 1 = total no of values that u wanted.
Hope this helped.
~A.

John G Duffy
July 16th, 2001, 08:00 AM
I believe you should be able to use the INPUT statement to read this file. It can handle comma delimited files. Here is an example program that writes some data to a file. The data ends up looking just like your sample. The program then reads this file back using the INPUT statement.
'
Start a new project, add two command buttons. Paste this code into the general declarations section of the form.
Change the strMyString variable in the Form_Load event to point to a valid disk location.
Run the program.
Click Command1 to create the file.
Use notepad to look at the newly created file.
Click Command2 to read the file back.

option Explicit
Dim strMyFile as string
Dim ffile
private Sub Command1_Click()
ffile = FreeFile()
Open strMyFile for Output as #ffile
' next statement writes comma delimited data.
Write #ffile, "abd", "DEF", "ghi", "JKL"
Close ffile
End Sub
'
private Sub Command2_Click()
Dim var1, var2, var3, var4
ffile = FreeFile()
Open strMyFile for input as #ffile
input #ffile, var1, var2, var3, var4
Close ffile
Debug.print var1; " "; var2; " "; var3; " "; var4

End Sub
'
private Sub Form_Load()
strMyFile = "C:\VB Stuff\Things to look at\Junk.txt"
End Sub




John G

Lorna
July 16th, 2001, 08:56 AM
Very helpful thanks

One question - what does Freefile() mean?

Iouri
July 16th, 2001, 09:28 AM
When you open file as #1 or #2 yoou might get an error because this number is already occupied by other process. FreeFile gives you the next available free number

Iouri Boutchkine
iouri@hotsheet.com

John G Duffy
July 16th, 2001, 11:42 AM
FreeFile() is a VB function that will return the next available file Number for use in file management statements such as Open, Close, Read Write, Input, Line Input etc.
It is documented in MSDN HELP along with examples.

John G