I think I am almost completed just missing one or two pieces I believe. If you could please just take a quick look to give your opinion I would be grateful.
Some background where I am at now with objects in Access
I have a linked table called DBO_VW_PPDREPORT off of this I built a query that is called qryPPDREPORT. This query is essentially the same as the view from SSMS with the data range choked down with a hard coded value ('2014-11')
The code is tagged below. The end game is to run the script to loop through the request and save off the filtered results into an Excel file. Pretty basic for a experts however I am not.
I did have a parameter set into the query (qryPPDREPORT) however I felt that may be problematic so I hard coded the value in the query.
Code is listed below which I found off of another Access site. Error message is run-time error 3061 two few parameters excepted two. ***I'm probably completely missing something.
Some background where I am at now with objects in Access
I have a linked table called DBO_VW_PPDREPORT off of this I built a query that is called qryPPDREPORT. This query is essentially the same as the view from SSMS with the data range choked down with a hard coded value ('2014-11')
The code is tagged below. The end game is to run the script to loop through the request and save off the filtered results into an Excel file. Pretty basic for a experts however I am not.
I did have a parameter set into the query (qryPPDREPORT) however I felt that may be problematic so I hard coded the value in the query.
Code is listed below which I found off of another Access site. Error message is run-time error 3061 two few parameters excepted two. ***I'm probably completely missing something.
HTML Code:
Private Sub Command9_Click() 'Help me :)
Dim qdf As DAO.QueryDef
Dim dbs As DAO.Database
Dim rstMgr As DAO.Recordset
Dim strSQL As String, strTemp As String, strMgr As String
' Replace PutEXCELFileNameHereWithoutdotxls with actual EXCEL
' filename without the .xls extension
' (for example, MyEXCELFileName, BUT NOT MyEXCELFileName.xls)
Const strFileName As String = "Please"
Const strQName As String = "zExportQuery"
Set dbs = CurrentDb
' Create temporary query that will be used for exporting data;
' we give it a dummy SQL statement initially (this name will
' be changed by the code to conform to each manager's identification)
strTemp = dbs.TableDefs(0).Name
strSQL = "SELECT * FROM [" & strTemp & "] WHERE 1=0;"
Set qdf = dbs.CreateQueryDef(strQName, strSQL)
qdf.Close
strTemp = strQName
' *** code to set strSQL needs to be changed to conform to your
' *** database design -- ManagerID and EmployeesTable need to
' *** be changed to your table and field names
' Get list of ManagerID values -- note: replace my generic table and field names
' with the real names of the EmployeesTable table and the ManagerID field
strSQL = "SELECT DISTINCT Supplier FROM qryPPDREPORT;" ' Getting error message here stating two few parameters
Set rstMgr = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
' Now loop through list of ManagerID values and create a query for each ManagerID
' so that the data can be exported -- the code assumes that the actual names
' of the managers are in a lookup table -- again, replace generic names with
' real names of tables and fields
If rstMgr.EOF = False And rstMgr.BOF = False Then
rstMgr.MoveFirst
Do While rstMgr.EOF = False
' *** code to set strMgr needs to be changed to conform to your
' *** database design -- ManagerNameField, ManagersTable, and
' *** ManagerID need to be changed to your table and field names
' *** be changed to your table and field names
strMgr = DLookup("SUPPLIER", "qryPPDREPORT", _
"SUPPLIER_ID = " & rstMgr!SUPPLIER_ID.Value)
' *** code to set strSQL needs to be changed to conform to your
' *** database design -- ManagerID, EmployeesTable need to
' *** be changed to your table and field names
strSQL = "SELECT * FROM qryPPDREPORT WHERE " & _
"SUPPLIER_ID = " & rstMgr!SUPPLIER_ID.Value & ";"
Set qdf = dbs.QueryDefs(strTemp)
qdf.Name = "q_" & strMgr
strTemp = qdf.Name
qdf.sql = strSQL
qdf.Close
Set qdf = Nothing
' Replace C:\FolderName\ with actual path
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
strTemp, "C:\MFG\" & strFileName & ".xls"
rstMgr.MoveNext
Loop
End If
rstMgr.Close
Set rstMgr = Nothing
dbs.QueryDefs.Delete strTemp
dbs.Close
Set dbs = Nothing
End Sub