UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

2 Pages V   1 2 >  
Reply to this topicStart new topic
> Export To Csv, Office 2010    
 
   
TimTDP
post Mar 31 2012, 06:31 AM
Post #1

UtterAccess Guru
Posts: 696



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
Go to the top of the page
 
+
Doug Steele
post Mar 31 2012, 06:49 AM
Post #2

UtterAccess VIP
Posts: 17,797
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.
Go to the top of the page
 
+
TimTDP
post Mar 31 2012, 07:06 AM
Post #3

UtterAccess Guru
Posts: 696



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
Go to the top of the page
 
+
Doug Steele
post Mar 31 2012, 07:07 AM
Post #4

UtterAccess VIP
Posts: 17,797
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.
Go to the top of the page
 
+
TimTDP
post Mar 31 2012, 07:09 AM
Post #5

UtterAccess Guru
Posts: 696



QUOTE (Doug Steele @ Mar 31 2012, 12:07 PM) *
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
Go to the top of the page
 
+
Doug Steele
post Mar 31 2012, 07:13 AM
Post #6

UtterAccess VIP
Posts: 17,797
From: Don Mills, ON (Canada)



What's the SQL of the query?
Go to the top of the page
 
+
TimTDP
post Mar 31 2012, 07:15 AM
Post #7

UtterAccess Guru
Posts: 696



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
Go to the top of the page
 
+
Doug Steele
post Mar 31 2012, 07:28 AM
Post #8

UtterAccess VIP
Posts: 17,797
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)?
Go to the top of the page
 
+
TimTDP
post Mar 31 2012, 09:53 AM
Post #9

UtterAccess Guru
Posts: 696



QUOTE (Doug Steele @ Mar 31 2012, 12:28 PM) *
Is frmPrintSupplierOrderResaleProducts open when you're trying to do the export (and does intOrderId on that form contain a value)?


frmPrintSupplierOrderResaleProducts is open
intOrderId does contain a value
Go to the top of the page
 
+
Doug Steele
post Mar 31 2012, 11:27 AM
Post #10

UtterAccess VIP
Posts: 17,797
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?
Go to the top of the page
 
+
TimTDP
post Mar 31 2012, 12:03 PM
Post #11

UtterAccess Guru
Posts: 696



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
Go to the top of the page
 
+
Doug Steele
post Mar 31 2012, 12:06 PM
Post #12

UtterAccess VIP
Posts: 17,797
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"
Go to the top of the page
 
+
TimTDP
post Apr 1 2012, 07:15 AM
Post #13

UtterAccess Guru
Posts: 696



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.

Go to the top of the page
 
+
Doug Steele
post Apr 1 2012, 08:06 AM
Post #14

UtterAccess VIP
Posts: 17,797
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"

Go to the top of the page
 
+
arnelgp
post 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
Go to the top of the page
 
+
TimTDP
post Apr 1 2012, 09:12 AM
Post #16

UtterAccess Guru
Posts: 696



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
Go to the top of the page
 
+
Doug Steele
post Apr 1 2012, 09:25 AM
Post #17

UtterAccess VIP
Posts: 17,797
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

Go to the top of the page
 
+
arnelgp
post 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
Go to the top of the page
 
+
TimTDP
post Apr 1 2012, 09:35 AM
Post #19

UtterAccess Guru
Posts: 696



QUOTE (Doug Steele @ Apr 1 2012, 02:25 PM) *
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!
Go to the top of the page
 
+
Doug Steele
post Apr 1 2012, 09:43 AM
Post #20

UtterAccess VIP
Posts: 17,797
From: Don Mills, ON (Canada)



Oops.

It should have been

CODE
  Open "C:\Folder\File.csv" For Output As #intFile


Go to the top of the page
 
+

2 Pages V   1 2 >
Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 18th June 2013 - 11:56 PM