473,471 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help Executing Access query.

I'm trying to export an Access table to a CSV file. I'm using this code:

Function ExportAccessToText() As Boolean

Dim AccessConn As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;"
& _
"Data Source=e:\My Documents\db1.mdb")

AccessConn.Open()

Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT *
INTO [Text;DATABASE=e:\My
Documents\TextFiles].[Table3.csv] FROM Table_3", AccessConn)

AccessCommand.ExecuteNonQuery()
AccessConn.Close()

End Function

Which gives this error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
in system.data.dll

On the line
AccessCommand.ExecuteNonQuery()

Any ideas what is wrong?

What is the file name produced by this syntax:
e:\My Documents\TextFiles].[Table3.csv]
Will it be named: e:\My Documents\TextFiles.Table3.csv

Thanks,
Brett
Nov 21 '05 #1
5 992
This code works fine but I'd like to know why the other code isn't working:

Dim fso = CreateObject("Scripting.FileSystemObject")
Dim txtfile As Object
txtfile = fso.OpenTextFile("C:\myFiles\Test\textfile.csv", 2, True)

Dim AccessConn As New
System.Data.OleDb.OleDbConnection("Provider=Micros oft.Jet.OLEDB.4.0;" & _
"Data Source=C:\myFiles\test\mydb.mdb")

AccessConn.Open()

Dim AccessCommand As New System.Data.OleDb.OleDbCommand("SELECT *
FROM [Table1]", AccessConn)

AccessCommand.ExecuteNonQuery()

Dim Reader As OleDbDataReader
Reader =
AccessCommand.ExecuteReader(CommandBehavior.CloseC onnection)
If Reader.Read() Then
While Reader.Read
txtfile.WriteLine(Reader.Item("List Number") & "," &
Reader.Item("List Name"))
End While
End If

AccessConn.Close()
Nov 21 '05 #2
Hi Brett,

Basically copy and paste the code below and should work
'dont forget might need to import namespsaces can't be bothered typing
the whole thing

These are the things done see the 2 points

1) use a connection string

Dim accessconn As New OleDbConnection()

Dim constring As String
constring = ("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\trid32.mdb")

2) Try using a OleDbDataAdapter

like below:

Dim objadapter As New OleDbDataAdapter()
With objadapter
.SelectCommand = New OleDbCommand()
.SelectCommand.CommandText = ("SELECT * INTO
[Text;DATABASE=C:\aaa\].[Table3.csv] FROM TABLE_3")
.SelectCommand.CommandType = CommandType.Text
.SelectCommand.Connection = New OleDbConnection(constring)
.SelectCommand.Connection.Open()
.SelectCommand.ExecuteNonQuery()
.SelectCommand.Connection.Close()
End With
Thanks
Adam

Nov 21 '05 #3
This seems to be quicker than my method. I still get

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
in system.data.dll

on the ExecuteNonQuery() line

I have to first make sure the file isn't there. That's the only time the
error is thrown.

Thanks,
Brett

"Adamz5" <ad****@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi Brett,

Basically copy and paste the code below and should work
'dont forget might need to import namespsaces can't be bothered typing
the whole thing

These are the things done see the 2 points

1) use a connection string

Dim accessconn As New OleDbConnection()

Dim constring As String
constring = ("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\trid32.mdb")

2) Try using a OleDbDataAdapter

like below:

Dim objadapter As New OleDbDataAdapter()
With objadapter
.SelectCommand = New OleDbCommand()
.SelectCommand.CommandText = ("SELECT * INTO
[Text;DATABASE=C:\aaa\].[Table3.csv] FROM TABLE_3")
.SelectCommand.CommandType = CommandType.Text
.SelectCommand.Connection = New OleDbConnection(constring)
.SelectCommand.Connection.Open()
.SelectCommand.ExecuteNonQuery()
.SelectCommand.Connection.Close()
End With
Thanks
Adam

Nov 21 '05 #4
There is only one table I can use from the Access database. All others
throw the error I posted earlier:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred

Any idea why that may be occurring or how I can troubleshoot it?

Thanks,
Brett

"Adamz5" <ad****@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi Brett,

Basically copy and paste the code below and should work
'dont forget might need to import namespsaces can't be bothered typing
the whole thing

These are the things done see the 2 points

1) use a connection string

Dim accessconn As New OleDbConnection()

Dim constring As String
constring = ("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\trid32.mdb")

2) Try using a OleDbDataAdapter

like below:

Dim objadapter As New OleDbDataAdapter()
With objadapter
.SelectCommand = New OleDbCommand()
.SelectCommand.CommandText = ("SELECT * INTO
[Text;DATABASE=C:\aaa\].[Table3.csv] FROM TABLE_3")
.SelectCommand.CommandType = CommandType.Text
.SelectCommand.Connection = New OleDbConnection(constring)
.SelectCommand.Connection.Open()
.SelectCommand.ExecuteNonQuery()
.SelectCommand.Connection.Close()
End With
Thanks
Adam

Nov 21 '05 #5

Hi Brett,

Use try catch as below, might also be worth checking you have the lates
data access cpmt installed MDAC 2.7

Use try

Catch exc As Exception
MsgBox(exc.Message)
End Try
Regards
Adam

Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
I have written 6 Queries in an Access DB, which are executed in a For Each…Next loop to populate DataSet object. A new DataTable object is created in DataSet for each query, as follows Private...
3
by: Neil Hindry | last post by:
I wonder if you can help me. I have setup an address-book database in Access XP. I have the first name & surname as separate fields. As I wanted to sort my database by surname and then by first...
5
by: Art | last post by:
Hi, Can anyone point me to an example of how I would execute a query I've created in an Access DB. I've copied the SQL down to my VB.net application and that works fine, but it's ugly. I'd like...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
8
by: daD | last post by:
I'm trying to write a small database that tracks people coming and going from a small campground. I need to have the current guests in the "current" table" and then have the ability to check them...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
8
by: SAL | last post by:
Hello, Using the designer in my project, I created a DataTable for one of the tables in an Access database. I created a TableAdapter for that DataTable by adding queries. For the most part, the...
1
by: jesmi | last post by:
my code is: U]employee.cfm <html> <head> <title>Employee List</title> </head> <body> <h1>Employee List</h1>
2
by: Randy | last post by:
Hi,all I met one problem of connecting Access database in asp.net web program. I use ODBC link to Access, the connection string like "PWD=12345;DSN=QADLinkMDB;DriverId=25;MaxBufferSize=2048;FIL=MS...
1
by: contactrajib | last post by:
I have a situation where one of the app server thread is executing an update statement on A record and at the same time another thread is trying to read it by executing a select query. Now we are...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.