WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADO DAO
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ADO Convert Access data to Tab Delimited Text File

 Keywords:  ADO Convert Access MDB Data Tab Comma Delimited CSV Text File ArrayFilePutCsv Array MicroSoft.Jet.OLEDB.4.0  

Question:

Is there a fast way of writing out an access table using SQL? The output file needs to be .txt (Tab delimited).

Answer:

The ADO GetRows method can be used to read the recordset into an array, then call ArrayFilePutCsv to write the data out to your file using the @TAB delimiter.
MDB = "C:\Temp\Data\Customers.mdb"
TXT = "C:\Temp\Data\Customers.txt"
TABLE = "Customers"
SQL = "SELECT * FROM [":TABLE:"]"
CONN = 'Provider=MicroSoft.Jet.OLEDB.4.0; Data Source=':MDB

oADO = ObjectCreate("ADODB.Connection")
oADO.Open(CONN)
oRS = oADO.Execute(SQL)
arrData = oRS.GetRows()  ; array of query results

oRS.Close()
oADO.Close()
oRs = 0
oADO = 0

ArrayFilePutCSV(TXT, arrData , @TAB )
Run(TXT,'')
Reference: ADO GetRows Method

Question:

Ran into a problem. My file has 100,000s of records and the arrDat=oRS.GetRows() wouldn't handle all of them. So I want to get a specific amount of rows and continue to do that until last recordset.Error I'm getting is:
COM Exception:
ADODB.Recordset
Current Recordset does not support bookmarks. This may be a limitation of the provider or of the selected cursortype.

Answer:

The code could look something like this:

MDB = "C:\Temp\Data\Customers.mdb"
TMP = "C:\Temp\Data\Temporary.txt"
TXT = "C:\Temp\Data\Customers.txt"
TABLE = "Customers"
SQL = "SELECT * FROM [":TABLE:"]"
CONN = 'Provider=MicroSoft.Jet.OLEDB.4.0; Data Source=':MDB

If FileExist( TXT ) Then FileDelete( TXT )

oADO = ObjectCreate("ADODB.Connection")
oADO.Open(CONN)
oRS = oADO.Execute(SQL)

While @TRUE
   If oRS.EOF Then Break ; no more records to get
   arrData = oRS.GetRows(10000,0) ; get limited number of records.
   ArrayFilePutCSV(TMP, arrData , @TAB ) ;write recordset array to temporary file
   FileAppend( TMP, TXT ); append temporary file to output txt file.
EndWhile

FileDelete( TMP ) ; delete temporary text file

oRS.Close()
oADO.Close()
oRs = 0
oADO = 0

Run(TXT,'')

Article ID:   W18031
Filename:   ADO Convert Access data to Tab Delimited Text File.txt
File Created: 2012:01:25:15:29:56
Last Updated: 2012:01:25:15:29:56