 |
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

February 26th, 2004, 10:31 AM
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Comma delimited text files
I am trying to read the records in a comma delimited text file but can't get past reading the first record. I tried the Get function but it says the Len doesn't match. The records are of varied sizes and I'm not sure how to get the length of a record without reading it first. I would like to be able to load each record into the text boxes in a form using command buttons and to add new records that I enter into the text boxes. The file structure is like this:
"aaaaaaaa", "bbbbb", "ccccccccccc"
"aaa", "bbbbbbbbbbb", "ccccc"
"aaaaaaaaaaaaa", "bbbbbbbbbbbbb", "cccc"
Nashville_Bill
__________________
Nashville_Bill
|

February 26th, 2004, 10:35 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there..
if you post what you have maybe we can try to fix it.. better that just telling you to use the filesystemobject or use lineinput...
Gonzalo Bianchi
|

February 26th, 2004, 10:42 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Use
Code:
Line Input #filenumber, varname
instead.
Your file open statement should look like this:
Code:
Open "filename" For Input As #filenumber
If you're interested you can alternatively use a Text Driver which allows you to treat a text file as a database. Let me know if you want more details of that approach.
hth
Phil
|

February 26th, 2004, 10:53 AM
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I use this code to get the first line of data into my form.
Private Sub cmdOpen_Click()
Dim strFilename As String
Dim strData0 As String
Dim strData1 As String
Dim strData2 As String
strFilename = "c:\data\data.txt"
Open strFilename For Input As #1
Input #1, strData0, strData1, strData2
txtData(0) = strData0
txtData(1) = strData1
txtData(2) = strData2
Close #1
lblRecordNumber = 1
End Sub
I'll try the "Line Input" in a loop to get to the next line of data.
Thanks for the help,
Bill
Nashville_Bill
|

February 26th, 2004, 11:01 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Bill, take care using hard-coded file numbers such as #1, it will trip you up sooner or later. Try to use the FreeFile function, like this:
Dim hFile
hFile = FreeFile
Open strFilename For Input As #hFile
rgds
Phil
|

February 26th, 2004, 11:16 AM
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok, I can get the record using Line Input, but I'm lazy and would like to get the fields seperately like I did using Input. Is there a way to do that, or should I just get over it an write the code to get the field data out of the record?
Private Sub cmdNext_Click()
Dim strFilename As String
Dim strData0 As String
Dim strData1 As String
Dim strData2 As String
Dim strRecNum As String
Dim i As Integer
strFilename = "c:\data\data.txt"
strRecNum = lblRecordNumber + 1
Open strFilename For Input As #1
Do Until i = strRecNum
Line Input #1, strData0
i = i + 1
Loop
txtData(0) = strData0
'txtData(1) = strData1
'txtData(2) = strData2
Close #1
lblRecordNumber = i
End Sub
Nashville_Bill
|

February 26th, 2004, 11:23 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
you can use instr() to get the position for every comma.. and then you can take out every field..
be carefull only if there is a comma inside "" because that will crash your string...
and like phil said, dont hardcode the file number.. it could be very painfull...
Gonzalo Bianchi
|

February 26th, 2004, 11:52 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Or Split() is useful in these circumstances. As Gonzalo says, it can become hard work if there are commas inside the data - then its time to start looking at the Text Driver :D
rgds
Phil
|

February 26th, 2004, 02:07 PM
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OK, I have completed the project now. It just seemed like there would be an easier way to do it.
Thanks for your help,
Bill
Nashville_Bill
|
|
 |