Link to home
Start Free TrialLog in
Avatar of myester
myester

asked on

How to import text file into MS Access database

I'm trying to import a comma delimited text file into a MS Access database with vb.net. The text file I want to import is named ACD5.txt. The database name is Support_Stats.mdb with one table named Calls. I'm trying the code below, but I'm getting the An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll error on the cmd.ExecuteNonQuery line. Please see the attached code.
Dim connect As String
        connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=U:\Manager\Support Team Measurement\Stats_Database\Support_Stats.mdb"
        Dim conn As New OleDbConnection(connect)
        Dim path As String = "U:\Manager\Support Team Measurement\Stats_Database\Support_Stats.mdb"
        Dim query As String = "INSERT INTO Calls (Week Starting, Inbound ACD Calls, Avg Inbound ACD Time) SELECT Week Starting, Inbound ACD Calls, Avg Inbound ACD Time FROM [Text;DATABASE=" & path & ";].[ACD5.txt]"
        Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of myester
myester

ASKER

Chaosian, that works great. Thanks for your help. I normally wouldn't use spaces but that's how it is in the text file so I thought the code needed to match that. I will remember to not use spaces. Thanks again.