I got some lucks that the lines I revised yours are green color in the vba module. But now I have a few questions:
a) I tried to execute the module but I got an message to ask me what the macro name is. How I can attach this module in an marco?
b) I think I still miss the codes to set and open recordset. Would you kindly to write me the codes? I am very brand new to access VBA
c) In your codes, it looks like the output's filename must be hardcoded in the code. Is it a way to have a dynamic filename so I don't need to go in and change the output's filename?
Below is my codes after I revised yours:
Public Function ExportMyProds(Plant As String, PSPK2 As String) As String
Dim db As DAO.Database, strSQL As String, strWHERE As String
' Set and Open Recordset here. The recordset will use qryDistinctProds (=T01_TransferMaster)
????
Set db = CurrentDb
' strSQL will be the sql string (i.e. in SQL view) from qryExport but without the WHERE clause.
' Notice the space after table1
strSQL = "SELECT Plant, Part, Desc, [Mat Tye], val_class, PSPK, PSPK2, NewMat, CurMat, [Matl Diff], [Mat Diff%], NewLbrBurd, CurLbrBurd, [Lbr Burd Diff], [Lbr Burd Diff%], NewFreight, CurFreight, [Freight Diff], [Freight Diff%], NewCostUnit, CurCostUnit, [Tot Cost Diff], [Tot Cost Diff%], MatStatus, PP1, PP1DTE, UoM, CK40N_OH, [CK40NReval$], MM03_OH, [MM03_Reval$], UsageQty, [UsageReval$], ShipQty, [ShipReval$], Note, Note2, [Profit Ctr], BU, BU2 FROM TABLE [Final COST CHG YOY] "
' Loop through Recordset here
strWHERE = "WHERE [Plant] = '" & param1 & "' AND [PSPK2] = '" & param2 & "';"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "T02Export", "C:\JE\YOY.xls", False
' Or use Docmd.TransferText
' Get Next value in Recordset
Set qdf = Nothing
Set db = Nothing
End Function