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