Solved

How to import text file into MS Access database

Posted on 2007-11-27
2
2,103 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:myester
2 Comments
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 20362036
Begin by putting [] around the field names. You can't have spaces in field names and expect them to behave well. (It won't take long before this is such a pain that you train yourself out of the habit of using spaces)
   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]"

    

Open in new window

0
 

Author Closing Comment

by:myester
ID: 31411310
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now