ADO & Flat File
Text files can also be accessed by using the ADO objects. Following is the code for the same:
'Connnection Objects
Dim conn
Set conn=CreateObject("ADODB.connection")
'Create the DSN for the text file, defualt directory is poiting to the location where flat file is kept
conn.open "DefaultDir=C:\Users\sandeep\Desktop\Blog R & D\ADO Flat File;Driver={Driver da Microsoft para arquivos texto (*.txt; *.csv)};DriverId=27;FIL=text;FILEDSN=C:\Users\sandeep\Desktop\Blog R & D\ADO Flat File\Flt.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
'Recordset object
Dim rs
Set rs=CreateObject("Adodb.recordset")
strQry="Select * from SampleTextFile.txt" ' Execute query
rs.Open strQry,conn
' Loop and get the records from the flat file
Dim counter
Do
counter= counter+1
For i=0 to rs.Fields.Count-1
If counter=1 Then
print rs(i).name
print rs(i).value
else
print rs(i).value
End If
Next
rs.MoveNext
Loop until rs.EOF
Set rs=Nothing
Set conn=Nothing
'Output Log
'Input Text File Log
'Connnection Objects
Dim conn
Set conn=CreateObject("ADODB.connection")
'Create the DSN for the text file, defualt directory is poiting to the location where flat file is kept
conn.open "DefaultDir=C:\Users\sandeep\Desktop\Blog R & D\ADO Flat File;Driver={Driver da Microsoft para arquivos texto (*.txt; *.csv)};DriverId=27;FIL=text;FILEDSN=C:\Users\sandeep\Desktop\Blog R & D\ADO Flat File\Flt.dsn;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
'Recordset object
Dim rs
Set rs=CreateObject("Adodb.recordset")
strQry="Select * from SampleTextFile.txt" ' Execute query
rs.Open strQry,conn
' Loop and get the records from the flat file
Dim counter
Do
counter= counter+1
For i=0 to rs.Fields.Count-1
If counter=1 Then
print rs(i).name
print rs(i).value
else
print rs(i).value
End If
Next
rs.MoveNext
Loop until rs.EOF
Set rs=Nothing
Set conn=Nothing
'Output Log
'Input Text File Log


Comments
Post a Comment