Hello forum goers! I have a sort of hopefully easy question that has been challenging me. Unfortunately, I cannot post the databases themselves due to confidental info, though I'd be happy to make a table up if needed. Basically I have two queries. One does this:
Code:
SELECT Product_NO,SBR,Order_ID
FROM 18MonthData
WHERE OrderID (Select Order_ID From 18MonthData WHERE Product_NO = [Product_Part])
This essentially finds all of the orders that a particular product part is associated with, then pulls all the information for those orders. The next query looks at the query and does this:
Code:
PARAMETERS [Product_ID] Text ( 255 );
TRANSFORM Count(SelectProdId.Order_No) AS CountOfOrder_No
SELECT SelectProdId.Product_NO
FROM SelectProdId
GROUP BY SelectProdId.Product_NO
ORDER BY SelectProdId.SBR
PIVOT SelectProdId.SBR;
It counts all the order numbers for each part and then creates a table where it associates part by number of orders. SBR is a marker variable here. Now, if it were just one part, it would work fine- it's doing that well. However, I need to do this for 1000 product parts, and only the top 10 of that table. So ideally I would love to automatically sort that table by the count of orders, since I am trying to create a VBA solution. (note: code is below)
Code:
Sub RunFiles()
Dim db As DAO.Database
Dim rsValues As DAO.Recordset
Dim rsXTB As DAO.Recordset
Dim qdfXTB As DAO.QueryDef
Dim InputTable As DAO.TableDef
Dim OutputTable As DAO.TableDef
Set db = CurrentDb()
Set OutputTable = db.TableDefs("Outputtable")
Set qdfXTB = db.QueryDefs("SelectProdID")
Set QueryTop10 = db.QueryDefs("SelectProdID_Crosstab")
Set rsValues = db.OpenRecordset("SELECT Product_Name FROM Top1000")
Set InputTable = db.TableDefs("Top1000")
DoCmd.SetWarnings (False)
'-- Process the values in ColumnName one at a time...
Do Until rsValues.EOF
'-- pass the parameter from rsValues into the rsXTB recordset as a filter
qdfXTB.Parameters(0) = rsValues.Fields("Product_Name")
QueryTop10.Parameters(0) = rsValues.Fields("Product_Name")
'Add product
Set rsXTB = QueryTop10.OpenRecordset
'InputTable.Fields("Product_Name").Value
'DoCmd.RunSQL ("INSERT INTO OutputTable VALUES ( " & rsValues.Fields("Product_Name") & ", Null , Null)")
rsXTB.Sort
For i = 2 To 11
rsXTB.MoveNext
DoCmd.RunSQL ("INSERT INTO OutputTable VALUES (" & rsXTB.Fields("Product_NO").Value & "," & rsXTB.Fields("1").Value & " ,1)")
Next i
rsXTB.Close
rsValues.MoveNext
Loop
rsXTB.Close
rsValues.Close
Set rsXTB = Nothing
DoCmd.SetWarnings (True)
End Sub
Is there a way to automatically sort the results of the crosstab? I can't do a sort in the query because it's a grouped variable. If not, should I pursue a VBA solution then?
Thanks in advance!