' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Runtime.InteropServices

Imports Access

Namespace E
    Public Class Program
        Public Shared Sub ExportOldWay(db As String, tbl As String, fnm As String)
            Dim ac As Application = New ApplicationClass()
            ac.OpenCurrentDatabase(db, False, "")
            Try
                ac.DoCmd.TransferText(AcTextTransferType.acExportDelim, Nothing, tbl, fnm, False, Nothing, 1252)
            Finally
                ac.CloseCurrentDatabase()
                ac.DoCmd.Quit(AcQuitOption.acQuitSaveNone)
                Marshal.ReleaseComObject(ac)
            End Try
        End Sub
        Public Shared Sub ExportNewWay(db As String, tbl As String, fnm As String)
            Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db)
                con.Open()
                Dim sw As New StreamWriter(fnm)
                Dim cmd As New OleDbCommand("SELECT * FROM " & tbl, con)
                Dim rdr As OleDbDataReader = cmd.ExecuteReader()
                While rdr.Read()
                    For i As Integer = 0 To rdr.FieldCount - 1
                        If i > 0 Then
                            sw.Write(";")
                        End If
                        If TypeOf rdr(i) Is String Then
                            sw.Write("""" & Convert.ToString(rdr(i)) & """")
                        Else
                            sw.Write(rdr(i))
                        End If
                    Next
                    sw.WriteLine()
                End While
                rdr.Close()
                sw.Close()
            End Using
        End Sub
        Public Shared Sub Main(args As String())
            ExportOldWay("C:\Databases\MSAccess\Test.mdb", "T1", "C:\z1.txt")
            ExportNewWay("C:\Databases\MSAccess\Test.mdb", "T1", "C:\z2.txt")
        End Sub
    End Class
End Namespace
