Solved

How to import text file into MS Access database

Posted on 2007-11-27
2
2,104 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

24 Experts available now in Live!

Get 1:1 Help Now