Updating database table from | delimeted text file

supersal666

Member
Joined
Aug 29, 2007
Messages
10
Location
Manchester, UK
Programming Experience
3-5
Hi everyone, I have an access table and I need to be able to update the table with data from a | delimeted text file.

The textfile is like below:

Card Number|Title|Forename|Surname|Address Line 1|Address Line 2|Address Line 3|Address Line 4|Address Line 5|PostCode|NOB|BarCode
123456|Mr|Mo|EYEOYIBO|43 Some Road|||Any Town|Kent|AB1 2CD|Plumber|0000200063571
654321|Mr|Terrence|Denman|29 Silly Avenue|||BlackTown|East Sussex|T22 7BG|Tiling / Kitchen & Bathroom Installation|0000200063623

At the moment I have some vb.net code which puts the data into a table (creates a new table) I actually need it to update the table (rather than create it) and seperate the values by the | character and put them into
the different fields.

VB.NET:
Module Module1

    Sub Main()

        'Function ImportTextToAccess()

        Dim myConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=mydatabase.mdb;")

        myConn.Open()

        Dim myCommand As New System.Data.OleDb.OleDbCommand("SELECT * INTO [Table3] FROM[Text;DATABASE=Text;DATABASE=C:\].[textfile.txt]", myConn)

        myCommand.ExecuteNonQuery()
        myConn.Close()

    End Sub

End Module
Has anyone got any sample code of how to add the data from the textfile using | as delimeters to put each value into the table into different fields?

Thanks
Sally
 
Last edited by a moderator:
Ok have worked this out now

first I replaced the | with a , and dropped the table:

VB.NET:
Module Module1

    Sub Main()

        Dim fso, inputFile, outputFile
        Dim str As String

        fso = CreateObject("Scripting.FileSystemObject")

        '1 means for reading

        inputFile = fso.OpenTextFile("mytextfile.txt", 1)

        str = inputFile.ReadAll

        'modify this string, replace required characters

        str = Replace(str, "|", ",")

        'write back

        outputFile = fso.CreateTextFile("C:\mytextfile.txt", True)

        outputFile.Write(str)

        Dim myConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\mydatabase.mdb;")

        myConn.Open()

        Dim myCommand As New OleDbCommand("DROP TABLE Table1", myConn)

        myCommand.ExecuteNonQuery()
        myConn.Close()

    End Sub
  
End Module

and then I created the table and inserted the data:

VB.NET:
Imports System.Data.OleDb

Module Module1

    Sub Main()

        Dim myConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=C:\mydatabase.mdb;")

        myConn.Open()

        Dim myCommand As New OleDbCommand("SELECT * INTO [Table1] FROM [Text;DATABASE=C:\;HDR=no;FMT=Delimited(,)].[mytextfile.txt]", myConn)

        myCommand.ExecuteNonQuery()
        myConn.Close()

    End Sub

End Module
 
Last edited by a moderator:
Back
Top