|
-
Oct 30th, 2006, 11:23 AM
#1
Thread Starter
Addicted Member
[RESOLVED] read a comma delimited txt file into a table
OK, I haven't worked with VB in about two years so I'm rusty and wasn't very good to begin with.
I have a .txt file that is storing a sequential records and need those records read into a vb table (15 colums, 81 rows). Any suggestions on where to start.
-
Oct 30th, 2006, 11:28 AM
#2
Re: read a comma delimited txt file into a table
What is "vb table" ? Some kind of grid? If so which one do you use?
-
Oct 30th, 2006, 12:17 PM
#3
Re: read a comma delimited txt file into a table
Something like this should do:
VB Code:
Private Sub Command1_Click()
Open FileName For Input As #1
Do While Not EOF(1)
Input #1, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15
'
'Add code here to fill these variables into your grid cells
'
Loop
Close #1
End Sub
-
Oct 30th, 2006, 01:49 PM
#4
Frenzied Member
Re: read a comma delimited txt file into a table
This creates a 2D array of strings.
You specify the file to load and what you want to use as a delimiter.
VB Code:
Function LoadCSV(Path As String, Optional Delim As String = ",") As String()
Dim ipf As Long
Dim strIn() As String
Dim strTmp() As String
Dim strOut() As String
Dim row As Long
Dim column As Long
If Dir(Path) <> "" Then 'test if file exists
ipf = FreeFile '
Open Path For Binary As #ipf 'open the file
ReDim strIn(0) '
strIn(0) = String(LOF(ipf), " ") 'create a buffer for the data
Get #ipf, , strIn(0) 'load the data into the buffer
strIn = Split(strIn(0), vbCrLf) 'split the data into individual lines
Close #ipf '
ReDim strOut(UBound(strIn), 0) '
For row = 0 To UBound(strOut) 'loop through all lines
strTmp = Split(strIn(row), Delim) 'split the line into individual fields
strIn(row) = vbNullString '
If UBound(strOut, 2) < UBound(strTmp) Then 'if the current line has more fields than our array has columns
ReDim Preserve strOut(UBound(strOut, 1), UBound(strTmp)) 'redim our array with more columns
End If '
For column = 0 To UBound(strTmp) 'write all fields to the array
strOut(row, column) = strTmp(column) '
Next column '
Next row '
End If
LoadCSV = strOut 'return the array
End Function
Last edited by jeroen79; Nov 1st, 2006 at 04:27 AM.
-
Oct 31st, 2006, 09:48 PM
#5
Thread Starter
Addicted Member
Re: read a comma delimited txt file into a table
Jeroen
The code worked lik a charm.
Do you think you or anyone could add comments. As I don't follow exactly what's going on.
My next step is to write from the table back to the csv.
-
Oct 31st, 2006, 10:18 PM
#6
Addicted Member
Re: read a comma delimited txt file into a table
http://www.vbforums.com/showthread.php?t=433735 might also help with your reading of *.csv files. (just look towards the end)
-
Nov 1st, 2006, 04:33 AM
#7
Frenzied Member
Re: read a comma delimited txt file into a table
-
Nov 1st, 2006, 08:35 AM
#8
Thread Starter
Addicted Member
Re: read a comma delimited txt file into a table
OK, how would you write the array back to the .csv keeping the same format?
stored in Array(x,x) as string
Array(0,0) = 1
Array(0,1) = 12
Array(0,2) = 123
Array(1,0) = a
Array(1,1) = ab
Array(1,3) = abc
Output to .csv
1,12,123
a,ab,abc
-
Nov 1st, 2006, 01:13 PM
#9
Frenzied Member
Re: read a comma delimited txt file into a table
-
Nov 6th, 2006, 06:44 AM
#10
Addicted Member
Re: read a comma delimited txt file into a table
http://www.vbforums.com/showthread.p...75#post2679775
Please have a look
 Originally Posted by jeroen79
This creates a 2D array of strings.
You specify the file to load and what you want to use as a delimiter.
VB Code:
Function LoadCSV(Path As String, Optional Delim As String = ",") As String()
Dim ipf As Long
Dim strIn() As String
Dim strTmp() As String
Dim strOut() As String
Dim row As Long
Dim column As Long
If Dir(Path) <> "" Then 'test if file exists
ipf = FreeFile '
Open Path For Binary As #ipf 'open the file
ReDim strIn(0) '
strIn(0) = String(LOF(ipf), " ") 'create a buffer for the data
Get #ipf, , strIn(0) 'load the data into the buffer
strIn = Split(strIn(0), vbCrLf) 'split the data into individual lines
Close #ipf '
ReDim strOut(UBound(strIn), 0) '
For row = 0 To UBound(strOut) 'loop through all lines
strTmp = Split(strIn(row), Delim) 'split the line into individual fields
strIn(row) = vbNullString '
If UBound(strOut, 2) < UBound(strTmp) Then 'if the current line has more fields than our array has columns
ReDim Preserve strOut(UBound(strOut, 1), UBound(strTmp)) 'redim our array with more columns
End If '
For column = 0 To UBound(strTmp) 'write all fields to the array
strOut(row, column) = strTmp(column) '
Next column '
Next row '
End If
LoadCSV = strOut 'return the array
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|