Question : Create CSV file from SQL and hard coded data

Hi,

I working on a small untility which exports data from an SQL database based on an SQL query.  The Final CSV file will be based on the Query results with some other specific 'hard-coded' data asl well.  How do i create a CSV file made up of SQL results and hard coded data?

For example:
CSV file:

1,2,3,4,5,6,7,8,910,11,12 (there are columns numbers)

Column 1 is hard coded with "SI"
Column 3 is based on a "variable"
Column 7 is hard coded with "sales invoice"
Column 9 is also based on a "variable"

All the rest will contain data returned from the SQL query.

I hope this makes sense.

Regards,
Tom

Answer : Create CSV file from SQL and hard coded data

Hi,

This is what i have figured out today:

Private Sub cmdExportData_Click()
Dim strSQL As String, db As Database, sField As String
Dim qdf As QueryDef
Dim DefaultNominalCode As String
Dim DefaultTaxCode As String
Dim InvoiceRun As Integer
Dim fpath As String
Dim fname As String
Dim file As String
Dim rs As DAO.Recordset


Set db = CurrentDb


 
InvoiceRun = Forms!frmExport!InvoiceRun
DefaultNominalCode = Forms!frmExport!DefaultNominalCode
DefaultTaxCode = Forms!frmExport!DefaultTaxCode

fpath = Forms!frmExport!Folder
fname = Forms!frmExport!FileName
file = fpath & "\" & fname & ".csv"

strSQL = "select 'SI' AS [Field1], c.[trCustomerID], '" & Replace(DefaultNominalCode, "'", "''") & "' AS [Field3], '' AS [Field4], i.[InvDate], i.[InvCode], 'Sales Invoice' AS [Field7], i.[Total_Principal], '" & Replace(DefaultTaxCode, "'", "''") & "' AS [Field9], i.[Total_Tax], '' AS [Field11], '' AS [Field12] FROM trCustomer c INNER JOIN trInvoiceHead i ON c.trCustomerID = i.trcustomerID WHERE [InvRunKey] = " & InvoiceRun

If strSQL = Null Then
MsgBox "No Results"
Else
 
        Set qdf = db.CreateQueryDef("qryExport", strSQL)
        DoCmd.TransferText acExportDelim, , "qryExport", file, False
        DoCmd.DeleteObject acQuery, "qryExport"
End If

End Sub

Tom
Random Solutions  
 
programming4us programming4us