Re: Csv data into vb script array
- From: "Richard Mueller" <rlmueller-NOSPAM@xxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 30 Dec 2006 23:42:24 -0600
Cuau wrote:
I need to know how to input a csv data into an array in visual basic
script.
I've used code similar to below in VB6:
============
' Specify comma delimited file.
strFile = "c:\MyFolder\input.csv"
' Declare array.
Dim arrRows() As String
' Open the file.
Open strFile For Input As #1
' Read the file.
k = 0
Do Until EOF(1)
Input #1, strField0, strField1, strField2, strField3
' Do what I want with the values.
ReDim Preserve arrRows(3, k)
arrRows(0, k) = strField0
arrRows(1, k) = strField1
arrRows(2, k) = strField2
arrRows(3, k) = strField3
k = k + 1
Loop
Close #1
===========
Look up help on Open, EOF, and Input. VB is very good at reading csv files
(unlike VBScript). You need to know how many fields are in the file. I
sometimes read the values into a disconnected recordset, so I can sort the
data. For example:
============
' Specify comma delimited file.
strFile = "c:\MyFolder\input.csv"
' Setup disconnected recordset. This requires reference to ADO.
Dim adoImport As ADODB.Recordset
Set adoImport = New ADODB.Recordset
adoImport.CursorType = adOpenKeyset
adoImport.LockType = adLockOptimistic
adoImport.Fields.Append "Field0", adVarChar, 50
adoImport.Fields.Append "Field1", adVarChar, 80
adoImport.Fields.Append "Field2", adVarChar, 100
adoImport.Fields.Append "Field3", adVarChar, 10
adoImport.Open
' Open the file.
Open strFile For Input As #1
' Read the file.
Do Until EOF(1)
Input #1, strField0, strField1, strField2, strField3
' Do what I want with the values.
adoImport.AddNew
adoImport("Field0").Value = strField0
adoImport("Field1").Value = strField1
adoImport("Field2").Value = strField2
adoImport("Field3").Value = strField3
adoImport.Update
Loop
Close #1
adoImport.Sort = "Field0"
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
.
- Prev by Date: Re: Between Keyword
- Previous by thread: Between Keyword
- Index(es):
Loading