Convert Access Table to Text File using VB

R

raju5725

have a MS access table and I want to export it to comma delimited
text file. How do I do this programmatically using VB.NET or C#?

Thanks for any help in advance.


Raju
 
P

Pragati Palewar

Hi,

For exporting Access table data, you need to read the records in
DataReader and then write these records in .CSV file one by one....

You need to follow these steps:

1. Open the file for writing (in append mode) with file stream writer:
Dim sw As New StreamWriter("FileName.csv", True)

2. Read table in DataReader.

3. Loop throught the data reader, Read record in string variable, Write
in File.

Dim strRecord as string

Do While myReader.Read()
' Use ',' (comma) as seperator when you are writing field values in
file.
strRecord = myReader("FIELD1") & "," & myReader("FIELD2") & ","
& myReader("FIELD2")......

' Write record in file.
sw.WriteLine(strRecord )
Loop

I don't know if there is some other way to do this.

Regards,

Pragati Palewar

Palewar Techno Solutions
Windows & Windows Mobile Software Development
Nagpur, India

http://www.palewar.com
 
P

Paul Clement

On 1 Sep 2006 23:16:51 -0700, (e-mail address removed) wrote:

¤ have a MS access table and I want to export it to comma delimited
¤ text file. How do I do this programmatically using VB.NET or C#?
¤
¤ Thanks for any help in advance.

You can do this rather easily with ADO.NET and SQL:

Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"
& _
"Data Source=e:\My Documents\db1.mdb")

AccessConn.Open()

Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO
[Text;HDR=No;DATABASE=e:\My Documents\TextFiles].[td.txt] FROM Table_3", AccessConn)

AccessCommand.ExecuteNonQuery()
AccessConn.Close()


Paul
~~~~
Microsoft MVP (Visual Basic)
 
A

aaron.kempf

wow. I am blown away.

Does that require Access to be on the machine? I've always been under
the assumption that the brackets were only available from within
MSACCESS.exe

-Aaron
 
H

hey.anand

I'm blown away as well, that's a pretty cool solution.

The one thing I feel like a complete idiot asking is the default file
export format is always CSV delimited, and I'd like my export to be a
TAB delimited text file. I've tried a zillion keywords to do this, all
with no luck. No help from google yet.

Could someone please let me know what I need to change in

SELECT * INTO
[Text;HDR=No;DATABASE=e:\My Documents\TextFiles].[td.txt] FROM Table_3

to generate the export in a TAB delimited format?

Thanks in advance.

wow. I am blown away.

Does that require Access to be on the machine? I've always been under
the assumption that the brackets were only available from within
MSACCESS.exe

-Aaron


Paul said:
On 1 Sep 2006 23:16:51 -0700, (e-mail address removed) wrote:

¤ have a MS access table and I want to export it to comma delimited
¤ text file. How do I do this programmatically using VB.NET or C#?
¤
¤ Thanks for any help in advance.

You can do this rather easily with ADO.NET and SQL:

Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"
& _
"Data Source=e:\My Documents\db1.mdb")

AccessConn.Open()

Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT* INTO
[Text;HDR=No;DATABASE=e:\My Documents\TextFiles].[td.txt] FROM Table_3", AccessConn)

AccessCommand.ExecuteNonQuery()
AccessConn.Close()


Paul
~~~~
Microsoft MVP (Visual Basic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top