tags:

views:

524

answers:

3

Hello,

I have an access database, with a query made. I need to automate it so that each night this query can run and export to a tab delimited csv file. It is not possible to export a query to a csv file from within access. My question is, are there any tools that can select certain tables, or perform an sql query on an mdb file, and export to a csv file?

+3  A: 

VBScript works quite well with the Jet engine. However, I do not see why you say " It is not possible to export a query to a csv file from within access."

 Sub TransferCSV()

    DoCmd.TransferText acExportDelim, , "PutNameOfQueryHere", "C:\PutPathAnd\FilenameHere.csv", True

 End Sub

Is the usual way in VBA.

EDIT: It is possible to run a VBScript file (.vbs) from the command line. Here is some sample VBScript to output a tab delimited file.

db = "C:\Docs\LTD.mdb"
TextExportFile = "C:\Docs\Exp.txt"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open _
   "Provider = Microsoft.Jet.OLEDB.4.0; " & _
   "Data Source =" & db

strSQL = "SELECT * FROM tblMembers"

rs.Open strSQL, cn, 3, 3

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.CreateTextFile(TextExportFile, True)

a = rs.GetString

f.WriteLine a

f.Close
Remou
In Access 2003 I can only export to textdata as ascii records, not a csv file.
Joshxtothe4
Have you tried DoCmd.TransferText acExportDelim?
Remou
I meant as a click-able option, I was not familiar enough with VB to attempt anything else.
Joshxtothe4
If you create a query and then choose file->export and select Save as Type: Text Files, you will get a dialog that will talk you through your export.
Remou
This is only true for tables for me, not queries. That is the first thing I tried.
Joshxtothe4
What goes wrong? Does you query work ok when you open it? Do you get an error message?
Remou
No error message, and query works fine. There is simply no option to get the dialog to export to a delimited format.
Joshxtothe4
I have edited my post to a snippet of code that can be cut and pasted into an Access module. Once done, change the "PutName.." text to the proper names for your set up. Run the snippet by choosing Run from the module menu.
Remou
Hi Remou, I cannot run this module, and can it be tab delimited, and run from a command line? I am sorry for my lack of familiarity.
Joshxtothe4
Remou
Hi Remou. I want to run a query and put the results into a tab delimited text file automatically from the command line. I can not seemingly do this within access however.
Joshxtothe4
There are two conflicting ideas here, however I have added some sample code that will run from the command line.
Remou
If you can't do it in Access, then you are obviously missing a lot of major features of Access, as it's easily accomplished either interactively or with the TransferDatabase command (table or query makes no difference). Perhaps you can describe what you're doing in Access that doesn't work.
David-W-Fenton
is vbscript preferred over access for security and performance reasons? I can still not get transfertext to work within the amcro..is tehre a way to make transfertext write to a unicode text file?
Joshxtothe4
Changing the specification to Unicode UTF-8 did nothing
Joshxtothe4
A: 

SQL Server Integration Services is able to do the transformation that you are talking about. Don't be fooled by the name, because you don't need SQL Server in order to automate and run the packages.

http://msdn.microsoft.com/en-us/library/ms141026.aspx

Nick Berardi
I am still reading over this, but have not yet got the gist. Is it a tool I would run and can schedule, or must I build a separate query from the one I have already?
Joshxtothe4
There is no need whatsoever to resort to SQL Server for something that is actually quite trivial to accomplish with Access by itself.
David-W-Fenton
+4  A: 

Actually, you can export a query to a csv file from within Access.

You can do this with a Macro using the TransferText method.

Macro:

   Name = ExportQuery
   Action = TransferText
   Transfer Type = Export Delimited
   Table Name = [name of your Access query]
   File Name = [path of output file]
   Has Field Names = [Yes or No, as desired]

You can execute the macro from the command line like this:

"[your MS Office path]\msaccess.exe" [your databse].mdb /excl /X ExportQuery /runtime

Since you're having trouble with TransferText in a macro try this:

1) Create a Module named "ExportQuery". In this module, create a function called "ExportQuery":

Function ExportQuery()
    DoCmd.TransferText acExportDelim, , "[your query]", "[output file].csv"
End Function

2) Create a Macro named RunExportQuery:

Action = RunCode
Function Name = ExportQuery ()
Patrick Cuff
I have tried this, but when creating a Macro I do not have the option to export delimited(in the macro builder) and I can not just put this into the visual basic editor. There is no way for me to set a parameter for TransferText, certainly not a tab stop.
Joshxtothe4
What version of Access are you using?
Patrick Cuff
Access 2003 standard. My copy is in german, so some things may be different.
Joshxtothe4
Maybe :( I'm using Access 2003 with all default settings and no add-ins. I edited my answer with another option to try.
Patrick Cuff
Hi Patrick, I have tried this again, and it does not seem to do anything. I don't understand why my copy cannot simply run the query, although searching a lot of people seem to have this. Is it possible to make it tab delimited. or is only comma possible?
Joshxtothe4
You can export as tab delimited, but you'd have to first create an export specification using the Export dialog that you can't run :( Catch-22.
Patrick Cuff
OK, I have it working now in a way. The error seems to be caused by unicode symbols in a record, at least that was the cause of not being able to export with vb script. Access gives this error: Syntax error in query. Incomplete query clause (Error 3450) when trying to save a stored query
Joshxtothe4
Is there a way to get transfertext to write to a unicode text file?
Joshxtothe4