My Assistant
![]() ![]() |
|
|
Mar 31 2012, 06:31 AM
Post
#1
|
|
|
UtterAccess Guru Posts: 677 |
How do I export a query to a .csv file?
how do I create the export specification. I am sure that Access 2010 supports this, I just can't find it! Thanks |
|
|
|
Mar 31 2012, 06:49 AM
Post
#2
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
Through the user interface, you pick Text File on the Export section of the External Data tab.
Programmatically, you use the TransferText action in a macro, or the DoCmd.TransferText method in VBA. |
|
|
|
Mar 31 2012, 07:06 AM
Post
#3
|
|
|
UtterAccess Guru Posts: 677 |
I get an error - too few parameters. Expected 1
But if I open the query the data is there! how do I create the export specification? This post has been edited by TimTDP: Mar 31 2012, 07:08 AM |
|
|
|
Mar 31 2012, 07:07 AM
Post
#4
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
I'm assuming you're exporting a query then. Do you get the same error simply running the query (as opposed to exporting it)? Or, more likely, you're getting a pop-up prompting you to enter a value.
|
|
|
|
Mar 31 2012, 07:09 AM
Post
#5
|
|
|
UtterAccess Guru Posts: 677 |
I'm assuming you're exporting a query then. Do you get the same error simply running the query (as opposed to exporting it)? Or, more likely, you're getting a pop-up prompting you to enter a value. I am exporting a query. When I open the query it opens perfectly. No pop-ups |
|
|
|
Mar 31 2012, 07:13 AM
Post
#6
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
What's the SQL of the query?
|
|
|
|
Mar 31 2012, 07:15 AM
Post
#7
|
|
|
UtterAccess Guru Posts: 677 |
CODE SELECT SupplierStockCodeOnly([ProductCode],[SupplierStockCodeReference],[SupplierStockCodeReferencePositionId]) AS [Item Code], qryReportSupplierOrderProducts.SumOfQuantity AS Quantity FROM tblSupplier INNER JOIN ((tblProduct INNER JOIN qryReportSupplierOrderProducts ON tblProduct.[ProductId] = qryReportSupplierOrderProducts.[ProductId]) INNER JOIN tblSupplierPurchaseOrderResale ON qryReportSupplierOrderProducts.[SupplierPurchaseOrderResaleId] = tblSupplierPurchaseOrderResale.[SupplierPurchaseOrderResaleId]) ON (tblSupplier.[SupplierID] = tblSupplierPurchaseOrderResale.[SupplierPurchaseOrderSupplierId]) AND (tblSupplier.[SupplierID] = tblProduct.[SupplierId]) WHERE (((tblSupplierPurchaseOrderResale.SupplierPurchaseOrderResaleId)=[Forms]![frmPrintSupplierOrderResaleProducts]![intOrderId])); The form "frmPrintSupplierOrderResaleProducts" is open, and correctly populated |
|
|
|
Mar 31 2012, 07:28 AM
Post
#8
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
Is frmPrintSupplierOrderResaleProducts open when you're trying to do the export (and does intOrderId on that form contain a value)?
|
|
|
|
Mar 31 2012, 09:53 AM
Post
#9
|
|
|
UtterAccess Guru Posts: 677 |
|
|
|
|
Mar 31 2012, 11:27 AM
Post
#10
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
You doing this through code or through the user interface?
I just tested doing it through VBA using Access 2010, and it worked without issue. If you are using code, what's the exact code you're using? |
|
|
|
Mar 31 2012, 12:03 PM
Post
#11
|
|
|
UtterAccess Guru Posts: 677 |
I want to do it with code!
Can you provide your code? With code I understand that I need to set an export specification. Is this correct? If not, so much the better! If I need to set an export specification, how do I do it? Many thanks |
|
|
|
Mar 31 2012, 12:06 PM
Post
#12
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
I didn't use a specification. (For simple csv, you usually don't need one).
All I did was CODE DoCmd.TransferText acExportDelim, , "MyQueryName", "C:\Folder\File.csv"
|
|
|
|
Apr 1 2012, 07:15 AM
Post
#13
|
|
|
UtterAccess Guru Posts: 677 |
Thanks. I notice that you do not include a specification.
When I remove the specification I get the run time error 3441 - Text file specification field separator matches decimal separator or text delimiter. |
|
|
|
Apr 1 2012, 08:06 AM
Post
#14
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
Where are you located? Are your regional settings such that you do use commas rather than decimal points?
If you do need a specification, you can create it manually. Start the Text Export through the GUI, and click on the Advanced button in the lower left of the wizard form. Once you've set the parameters correctly, there's a Save button you can click to save the specification. You'd then put the name of the specification you created between the two commas: CODE DoCmd.TransferText acExportDelim, "MySpecification", "MyQueryName", "C:\Folder\File.csv" |
|
|
|
Apr 1 2012, 08:17 AM
Post
#15
|
|
|
UtterAccess Ruler Posts: 1,090 |
If I may jump in your discussion.
You can use this code to import to text file. Just make a reference to Microsoft ActiveX Data Object X.0 Library Dim cn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim sSql As String Dim strA As String 'Dim fso As New Scripting.FileSystemObject 'Dim txtFile As Scripting.TextStream Dim fso As Object Dim txtFile As Object Set fso = CreateObject("Scripting.FileSystemObject") Set cn = CurrentProject.Connection sSql = currentdb.QueryDefs("YourQueryDefinition").SQL '<< put the actual name of your query 'Fires the query and then writes the output to a flat text file 'TAB delimeted rst.Open sSql, cn ' get the Column Header For i = 0 To rst.Fields.Count - 1 strA = strA & rst.Fields(i).Name & vbTab Next strA = Left(strA, Len(strA) - 1) & vbCrLf Set txtFile = fso.CreateTextFile("C:\Test.txt", True) '<< path and file name to save. 'Write the Header (Column Name) txtFile.Write (strA) 'Write the Data (rows) txtFile.Write (rst.GetString(adClipString, , vbTab, _ '<< I used TAB as delimeter, you can put yours vbCrLf, "")) Set txtFile = Nothing Set rst = Nothing Set cn = Nothing Set fso = Nothing |
|
|
|
Apr 1 2012, 09:12 AM
Post
#16
|
|
|
UtterAccess Guru Posts: 677 |
How can I just write the data to a file?
something like: Open file #1 open recordset loop through it, writing each record to the file Close Recordset Close file #1 |
|
|
|
Apr 1 2012, 09:25 AM
Post
#17
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
Never hard-code the file number, as it's possible some other process may be using that same number: always use the FreeFile function to get a file handle.
Try something like: CODE Dim qdf As DAO.QueryDef Dim prm As DAO.Parameter Dim rs As DAO.Recordset Dim fld As DAO.Field Dim intFile As Integer Dim strBuffer As String Set qdf = CurrentDb.QueryDefs("MyQuery") For Each prm in qdf.Parameters prm.Value = Eval(prm.Name Next prm Set rs = qdf.OpenRecordset intFile = FreeFile() Open "C:\Folder\File.csv" As #intFile While rs.EOF = False strBuffer = vbNullString For Each fld In rs.Fields strBuffer = strBuffer & fld.Value & "," Next fld strBuffer = Left$(strBuffer, Len(strBuffer) - 1) Print #intFile, strBuffer rs.MoveNext Loop rs.Close Set rs = Nothing Set fld = Nothing Set prm = Nothing Set qdf = Nothing Close #intFile |
|
|
|
Apr 1 2012, 09:33 AM
Post
#18
|
|
|
UtterAccess Ruler Posts: 1,090 |
you don't need to do anything just run the code.
here I made it into a function. Just insert the code in a New Module or Existing Module. CODE Public Enum agpSelectObjectType
agp_TableName agp_SelectSQLString agp_QueryDefinitionName End Enum ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Procedure : SelectToText ' ' Parameters: ' ' strAny Can be name of a Query, Table Name or a Select Statement ' varType One of the type in the private enum. ' strPathAndFileName Full path and name of file you want to save the export ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub SelectToText(strAny As String, varType As agpSelectObjectType, strPathAndFileName As String) Dim cn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim sSql As String Dim strA As String 'Dim fso As New Scripting.FileSystemObject 'Dim txtFile As Scripting.TextStream Dim fso As Object Dim txtFile As Object On Error GoTo SelectToText_Error Select Case varType Case Is = SelectObjectType.agp_TableName sSql = "Select * From " & strAny & ";" Case Is = SelectObjectType.agp_QueryDefinitionName sSql = CurrentDb.QueryDefs(strAny).SQL Case Else sSql = strAny End Select Set fso = CreateObject("Scripting.FileSystemObject") Set cn = CurrentProject.Connection 'Fires the query and then writes the output to a flat text file 'pipe delimeted rst.Open sSql, cn ' get the Column Header For i = 0 To rst.Fields.Count - 1 strA = strA & rst.Fields(i).Name & vbTab Next strA = Left(strA, Len(strA) - 1) & vbCrLf Set txtFile = fso.CreateTextFile(strPathAndFileName, True) 'Write the Header (Column Name) txtFile.Write (strA) 'Write the Data (rows) txtFile.Write (rst.GetString(adClipString, , vbTab, _ vbCrLf, "")) Set txtFile = Nothing Set rst = Nothing Set cn = Nothing Set fso = Nothing On Error GoTo 0 Exit Sub SelectToText_Error: MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure SelectToText of Module Module6" End Sub |
|
|
|
Apr 1 2012, 09:35 AM
Post
#19
|
|
|
UtterAccess Guru Posts: 677 |
Never hard-code the file number, as it's possible some other process may be using that same number: always use the FreeFile function to get a file handle. Try something like: CODE Dim qdf As DAO.QueryDef Dim prm As DAO.Parameter Dim rs As DAO.Recordset Dim fld As DAO.Field Dim intFile As Integer Dim strBuffer As String Set qdf = CurrentDb.QueryDefs("MyQuery") For Each prm in qdf.Parameters prm.Value = Eval(prm.Name Next prm Set rs = qdf.OpenRecordset intFile = FreeFile() Open "C:\Folder\File.csv" As #intFile While rs.EOF = False strBuffer = vbNullString For Each fld In rs.Fields strBuffer = strBuffer & fld.Value & "," Next fld strBuffer = Left$(strBuffer, Len(strBuffer) - 1) Print #intFile, strBuffer rs.MoveNext Loop rs.Close Set rs = Nothing Set fld = Nothing Set prm = Nothing Set qdf = Nothing Close #intFile Print #intFile, strBuffer give error 54 - bad file mode Otherwise this works great! |
|
|
|
Apr 1 2012, 09:43 AM
Post
#20
|
|
|
UtterAccess VIP Posts: 17,618 From: Don Mills, ON (Canada) |
Oops.
It should have been CODE Open "C:\Folder\File.csv" For Output As #intFile |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 20th May 2013 - 03:43 PM |