Link to home
Start Free TrialLog in
Avatar of vibale
vibale

asked on

opening / Reading files using directorylistbox and filelistbox?

I currently have a form that contains a directorylistbox, a filelistbox and a command button. I would like the command button to open / read the chosen file. This file would be a comma seperated file. I would then like to sort the data etc, and save different instanes of it with different names. I know how to do the last bit, but im not sure how to open / read the file, and how to distinguish what fields are what, using comma seperation.
Avatar of jrspano
jrspano
Flag of United States of America image

get you path and file name then


dim holder as variant
Open "path\MyFile1" For Input As #1
Line Input #1,sLine1 'this gets the first line
holder = split(sline1,"'")
close #1
'holder is an array of the line now
example
if you have a,b,ddd then holder is
holder(0) is a, (1) is b, (2) is ddd etc
you then do what you need with it.

another way is to loop throught the line looking for the "," though the split is cleaner.  make a do loop around the line input and holder to get all the lines

do unit eof(#1)

loop
that eof(#1) should have been eof(1)
Avatar of vibale
vibale

ASKER

Sorry, I'm Very new to Programming, and I dont seem to understand. This is what my command button does at the moment.

pathtofile = File1.Path & "\" & File1.FileName
Dim holder As Variant
Open pathtofile For Input As #1
Line Input #1, sline1
holder = Split(sline1, "'")
Close #1

I know that more code needs to go in there, but im not sure how. Now it seems to open the file, i need it refer to 1st field as something , the second field as something else and also process each line at a time. My guess is that it will use the do until...Loop command.
yes you need the do loop command.  You can't refer to delimited text docs as "somethings"  You have to pull it line by line and do something with each line.  You  could use ADO to make a record set and then load it from the text file.  what are you going to do with the info in the file?


here is your example changed a little

pathtofile = File1.Path & "\" & File1.FileName
Dim holder As Variant
Open pathtofile For Input As #1
do until eof(1)
  Line Input #1, sline1
  holder = Split(sline1, "'")
  'do something with holder here.
loop
Close #1
this will read the file line by line and do what ever you want to on each line.  if you want to use the file like a database let me know and I'll give you a quick example on ADO.
Avatar of vibale

ASKER

Thats exactly what I want it to do, act like a database. It will then get the first part of the text, strip it, and somebits in, do something with the text after the comma, and then save a file, using the first part of text as the filename. I.E If there were 100 lines in the text file, then the program would generate 100 files (Probably). Although I might change this.
Avatar of vibale

ASKER

The most important thing is knowing how to read the file, and how to use the data within it.
just reading the data and using it like a db is easy.  it is VERY hard to get the updated data back in to the same file though.  you don't know where each string came from.  if you want to make a record set out of the file here's how

set a reference to ms Active x data objects 2.5 or 2.6 if you have it

in code 3 column example
txt file
a,aa,aaa
b,bb,bbb
c,cc,ccc

dim rs as new adodb.recordset
with rs
.Fields.Append "FirstField", adChar, 36, adFldIsNullable
.Fields.Append "SecondField", adChar, 8, dFldIsNullable
.Fields.Append "ThirdField", adChar, 20, adFldIsNullable
'1st is a character field of 36 chars, 2nd is 8 chars, 3rd is 20 chars all allow nulls
end with

pathtofile = File1.Path & "\" & File1.FileName
Dim holder As Variant
Open pathtofile For Input As #1
do until eof(1)
 Line Input #1, sline1
 holder = Split(sline1, "'")
 rs.addnew
 rs!firstField = cstr(Holder(0))
 rs!secondfield = cstr(Holder(1))
 rs!thirdfield = cstr(Holder(2))
 rs.update
loop
Close #1

now you have a recordset of the data and can use it

ex
dim s as string

rs.movefirst
s = rs!thirdfield 's = aaa
rs.movenext
s = rs!secondfield 's = bb
rs.movenext
s = rs!thirdfield 's = ccc

rs.close 'close the recordset

there are other ways i believe to get the txt file straight into the recordset using ado 2.6 but you will have to look in to that if you want to persue that route.  I don't know how I haven't needed that.
also i left out the .open right above the end with. oops
should be
with rs
.Fields.Append "FirstField", adChar, 36, adFldIsNullable
.Fields.Append "SecondField", adChar, 8, dFldIsNullable
.Fields.Append "ThirdField", adChar, 20, adFldIsNullable
.open
'1st is a character field of 36 chars, 2nd is 8 chars, 3rd is 20 chars all allow nulls

end with
Avatar of vibale

ASKER

Im not sure what you mean by;

set a reference to ms Active x data objects 2.5 or 2.6 if you have it

in code 3 column example
txt file
a,aa,aaa
b,bb,bbb
c,cc,ccc

The rest of it I have done, but when I run it, I get an error, which I think is related to me not doing what the above says. What do I do with the above?
Avatar of vibale

ASKER

Im not sure what you mean by;

set a reference to ms Active x data objects 2.5 or 2.6 if you have it

in code 3 column example
txt file
a,aa,aaa
b,bb,bbb
c,cc,ccc

The rest of it I have done, but when I run it, I get an error, which I think is related to me not doing what the above says. What do I do with the above?

set a reference to ms Active x data objects 2.5 or 2.6 if you have it
goto project menu then references.  check the reference to microsoft activex recordset object 2.6(or what ever version you have)  if you don't have at least 2.5 you should get the upgrade from ms.


'this is used just as an example below in above comment. if you have a txt file with the following in it.
in code 3 column example
txt file
a,aa,aaa
b,bb,bbb
c,cc,ccc
Avatar of vibale

ASKER

What site can I get an upgrade from? I only have Microsoft ActiveX Data Objects Recordset 2.1 Library.
get it from ms

http://www.microsoft.com/data/download.htm

get the mdac 2.6 sp1 download and install it

2.1 will work but 2.5 and 6 add some really good features.
Avatar of glass_cookie
Hi!

Here's 2 files over the net from you - sorry, I'm not going to read everything in this page:

Download...
http://www.vb-helper.com/Howto/startdef.zip
Description: Open a file using the application appropriate for its extension (.doc, .txt, .htm, etc.) (3K)

Download...
http://www.vb-helper.com/Howto/countfld.zip
Description: Total the values in a specific field in a comma-delimited text file (3K)

That's it!

glass cookie : )

PS. Ignore me if I'm out of point or if this is NA.
Avatar of vibale

ASKER

The previous error disappeared, but I know get different ones, most of which I've sorted, but I can work this one out. It says.

Subscript out of range

and when I hover over the line in my code where it stops, it says:

CStr(holder(2)) = <Subscript out of range>

any ideas?
Avatar of vibale

ASKER

Ive also changed the line that says:

Split(sline1, "'")

to

Split(sline1, ",")
the subscript is about the array holder

it is 0 based

example

s = "a,b,c"
holder = split(s,",")

holder(0) = a
holder(1) = b
holder(2) = c
you are also right in changeing the ' to a , in the split line.  I made a typo
Avatar of vibale

ASKER

What do I need to do. I can't seem to see anything wrong. What do you mean by it being 0 based.
0 based is the array number the array starts on.  some things in vb are 1 based and start on 1 so if you tried to do something like onebasedarray(0) you get the subscript out of range when you get the error what does your text file look like?  if you have any blank lines you will only get Holder(0) not 1 and 2 etc.  you need to check for this or handle the error.
Avatar of vibale

ASKER

Thats great, that works fine. One final question. Is there anyway of returning an error to the user if the file they choose to use as input is not valid. I.E it doesnt have enough fields, or it has too many fields, or it isnt comma sepearted. Is there any way od detecting this?
ASKER CERTIFIED SOLUTION
Avatar of jrspano
jrspano
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vibale

ASKER

Thanks alot. That was really helpful.