Re: How to read data from MS Access? by ekkehard
ekkehard
Thu Jan 24 16:07:51 CST 2008
D.P. Roberts schrieb:
> I have a vbs file that uses a Do loop to read through hundreds of rows in a
> csv file. There are 6 columns in each row and the values are assigned like
> this:
>
> arrUserInfo = split(objTextFile.ReadLine,",")
>
> strFirst = arrUserInfo(0)
> strSecond = arrUserInfo(1)
> strThird = arrUserInfo(2)
> strFourth = arrUserInfo(3)
> strFifth = arrUserInfo(4)
> strSixth = arrUserInfo(5)
>
> This works fine but I would rather have my vbs file read the data from the
> original source, which is a table in MS Access.
> Does anyone know how to loop through an Access table and assign values like
> this in vbs?
>
> Thanks!
>
>
A simple demo:
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
' Config: change according to your system
' sMFSpec should point to your northwind.mdb resp. the .mdb you what to use
Dim sMFSpec : sMFSpec = oFS.GetAbsolutePathName( ".\impadoexcel\Nwind.mdb" )
Dim oADOCN : Set oADOCN = CreateObject( "ADODB.Connection" )
Dim sCS : sCS = Join( Array( _
"Provider=Microsoft.Jet.OLEDB.4.0" _
, "Data Source=" & sMFSpec _
), ";" )
Dim sSQL : sSQL = Join( Array( _
"SELECT * FROM Categories" _
), " " )
oADOCN.Open sCS
Dim oRS : Set oRS = oADOCN.Execute( sSQL )
Dim strFirst, strSecond
Do Until oRS.EOF
strFirst = oRS.Fields( 0 ).Value
strSecond = oRS.Fields( "CategoryName" ).Value
WScript.Echo strFirst , "==", oRS.Fields( 0 ).Value
WScript.Echo strSecond, "==", oRS.Fields( "CategoryName" ).Value
oRS.MoveNext
Loop
oADOCN.Close