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?

Are you sure you want to do that? That sounds so 1980ish :)

Anyways you could just create a class the represents each table in the database and override the .ToString() method and create a static length string.

Yes i'm sure. Their is a program that will be using the text file, so the text file can't be modified while it's in use. So I need the code for exporting the file at the click of a button.

You can open a file that is in use -- you just have to do it a special way. Here is the C# code but you should be able to port it to VB.NET easily enough. The FileStream() overload lets you open a file readonly while another application has it open for read/write.

using (FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
        using (StreamReader sr = new StreamReader(fs))
        {

          string line;
          int lineNumber = 0;
          while ((line = sr.ReadLine()) != null)
          {

If that stops you from having to write a fixed length file then you should be good to go, right? You can modify the above code to copy the file if its a database .mdb file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.