J
Junior728
Hi,
I am using the Form method, trying to open up a query, queries by a user
input into the form textbox, then will run thru the whole table and only show
what the user wants, and finally transfer the data into excelbk and save in
folder for viewing.
i think i am stuck in defining if the user input string to be equal to the
column field string(customer pn) in the table,then show those.
So when i execute my query, its generates the whole table.
can someone help me?
=================
Option Compare Database
Public Sub CopyRecordset2XL()
Dim objXLApp As Object
Dim objXLWb As Object
Dim objXLWs As Object
Dim strWorkBook As String
Dim strWorkSheet As String
Dim lngSheets As Long 'sheet number
Dim lngCount As Long 'counter
Dim MyDB As Database
Dim RecordMRP As Recordset
Dim QueryMRP As QueryDef
Dim strSQL As String
Dim Customer_PN As String
strWorkBook = "\\txfil001\MKoh$\My WorkStation\MRPbyPN.xls"
Set QueryMRP = CurrentDb.QueryDefs("Year 2007 FC Query by Cust, by
CPN-Done by ML")
strSQL = [Forms]![QuerybyYear]![CustPN]
'With QueryMRP
strSQL = Customer_PN ====> Stuck here!
'End With
'strSQL = [Forms]![Name of Form]![Name of TextBox]
Set RecordMRP = QueryMRP.OpenRecordset(dbOpenDynaset)
Set objXLApp = CreateObject("Excel.Application")
'name and full path to use to save the xls file
'only create workbooks with 1 sheet
lngCount = objXLApp.SheetsInNewWorkbook 'save user's setting
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = lngCount 'restore user's setting
strWorkSheet = "Sheet1"
Set objXLWs = objXLWb.Worksheets(strWorkSheet)
objXLWs.Range("A2").CopyFromRecordset RecordMRP 'I want to copy start
from Header!
objXLWs.Columns.AutoFit
'Save wb
objXLWb.Save
objXLWb.Close
'Need to close off the Excel Book Object
Set objXLWs = Nothing
Set objXLApp = Nothing
' Destroy the recordset object
RecordMRP.Close
End Sub
I am using the Form method, trying to open up a query, queries by a user
input into the form textbox, then will run thru the whole table and only show
what the user wants, and finally transfer the data into excelbk and save in
folder for viewing.
i think i am stuck in defining if the user input string to be equal to the
column field string(customer pn) in the table,then show those.
So when i execute my query, its generates the whole table.
can someone help me?
=================
Option Compare Database
Public Sub CopyRecordset2XL()
Dim objXLApp As Object
Dim objXLWb As Object
Dim objXLWs As Object
Dim strWorkBook As String
Dim strWorkSheet As String
Dim lngSheets As Long 'sheet number
Dim lngCount As Long 'counter
Dim MyDB As Database
Dim RecordMRP As Recordset
Dim QueryMRP As QueryDef
Dim strSQL As String
Dim Customer_PN As String
strWorkBook = "\\txfil001\MKoh$\My WorkStation\MRPbyPN.xls"
Set QueryMRP = CurrentDb.QueryDefs("Year 2007 FC Query by Cust, by
CPN-Done by ML")
strSQL = [Forms]![QuerybyYear]![CustPN]
'With QueryMRP
strSQL = Customer_PN ====> Stuck here!
'End With
'strSQL = [Forms]![Name of Form]![Name of TextBox]
Set RecordMRP = QueryMRP.OpenRecordset(dbOpenDynaset)
Set objXLApp = CreateObject("Excel.Application")
'name and full path to use to save the xls file
'only create workbooks with 1 sheet
lngCount = objXLApp.SheetsInNewWorkbook 'save user's setting
objXLApp.SheetsInNewWorkbook = 1 'set for only 1 sheet
Set objXLWb = objXLApp.Workbooks.Open(strWorkBook)
objXLApp.SheetsInNewWorkbook = lngCount 'restore user's setting
strWorkSheet = "Sheet1"
Set objXLWs = objXLWb.Worksheets(strWorkSheet)
objXLWs.Range("A2").CopyFromRecordset RecordMRP 'I want to copy start
from Header!
objXLWs.Columns.AutoFit
'Save wb
objXLWb.Save
objXLWb.Close
'Need to close off the Excel Book Object
Set objXLWs = Nothing
Set objXLApp = Nothing
' Destroy the recordset object
RecordMRP.Close
End Sub