Click to See Complete Forum and Search --> : Export Microsoft Access Database to Text File


cass84@gmail.com
August 7th, 2009, 08:54 AM
Hi, I'm using Visual Basic Express 2008 (VB.NEt). I've created a project which
connect to a Microsoft Access Database (OLEDB) & displays the records using textboxes in the windows app.

I need to add a button which exports the database to a fixed length text file. Does anyone know the code I would use to do this?

DataMiser
August 7th, 2009, 10:42 AM
There are several ways to do it.

One method would be to first create a string that contains the data for a given record by padding each field to the desired lenght and concantonating them together. Then use one of the file or streamwriter methods to write the string to the choosen file.

Here is a little sub routine from one of my old VS2003 projects which I use to write a line to a file as data is collected.

Private Sub FileWrite(ByVal fText As String)
Dim ofile As New System.IO.StreamWriter("\ToolCrib.txt", True)
ofile.WriteLine(ftext)
ofile.Close()
End Sub

cass84@gmail.com
August 7th, 2009, 11:00 AM
But I don't want it to create the file until the button is clicked. once the button is clicked I want it to take the data from the database & create the fixed length text file.

DataMiser
August 7th, 2009, 02:52 PM
Of course.. That was just an example of how to write a line to a file. To do what you want simply use a loop to write all the lines to the file before you close it. Everything you need to know about how to write the data to the file is there. I assume you already know how to get the data fromt he database.

cass84@gmail.com
August 7th, 2009, 02:56 PM
Bad assumption. I am just a beginner here. Which is why i'm asking for help with the code. I'm not sure where to begin or end.

Shuja Ali
August 7th, 2009, 04:01 PM
In the button's click event handler, open the connection to the database and using the Datareader read the data and the code in Post #2 shows how to to write it to the text file. It should be straight forward as long as you know how to connect to the access database and how to open a datareader for reading the data.