I'm trying to teach myself VB and Ive been successful in writing a very similar module, but for some reason this one just will not run. I have no Idea why the nested SQL Select statement will not recognize my fields. I keep getting the following run-time error "You tried to execute a query that does not include the specified expression 'League' as part of an aggregate function". I know that the field exists because it is from an existing query that I currently run. I would greatly appreciate any help on this one. P.S. I re-wrote the SQL in build-up method thinking that it would get the thing to run, but not so lucky. Below is my code for your review.
Code:
Public strInput As String
Sub GetExcessData_With_SQL_GetObject_With_Excel()
'Step 1: Declare your Variables
Dim MyConnect As String
Dim MyRecordset As ADODB.Recordset
Dim MyQueryDef As DAO.QueryDef
Dim MyDatabase As DAO.Database
Dim MySQL As String
Dim MyRange As String
Dim Db As Database
Dim xl As Excel.Application
Set xl = CreateObject("Excel.Application")
Dim xlwkbk As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim strInput As String
Dim strMsg As String
'Sep 2: Declare your connection string
MyConnect = "Provider=Microsoft.ACE.OLEDB.12.0; Persist Security Info = False;Data Source= Q:\WWC Common\TAMPA Files\LSG Month-End FG Inventory Excess-Reserve Analysis.accdb; User ID = Admin;"
'Step 3: Build Input Box for Fiscal year
strMsg = "What Fiscal Month?"
strInput = InputBox(Prompt:=strMsg, Title:="Month")
strInput = Chr(34) & strInput & Chr(34)
'Step 4: Build Input Box for Fiscal Month ,'
'Step 5: Build Your SQL Statement
MySQL = "SELECT [slq-Cnt Mnth Calc reserveb].[Fiscal Year], [slq-Cnt Mnth Calc reserveb].[Import Period],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Reporting Month], [slq-Cnt Mnth Calc reserveb].[League],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Category],[slq-Cnt Mnth Calc reserveb].[Group Name],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Style],[slq-Cnt Mnth Calc reserveb].[Style Description],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Color],[slq-Cnt Mnth Calc reserveb].[Color Desc],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Grs Inv Units],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Grs Inv $],[slq-Cnt Mnth Calc reserveb].[90 day demand Units],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[90 day demand $],[slq-Cnt Mnth Calc reserveb].[90 day Excess Units],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[$90 day Excess],[slq-Cnt Mnth Calc reserveb].[Opt2 F Grs],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[Opt2 F Grs$],[slq-Cnt Mnth Calc reserveb].[1st qlty Excess],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[$1st qlty Excess],[slq-Cnt Mnth Calc reserveb].[$1st Qlty Rsv],"
MySQL = MySQL & "[slq-Cnt Mnth Calc reserveb].[$Risk Rsv],[slq-Cnt Mnth Calc reserveb].[$Total Rsv],[slq-Cnt Mnth Calc reserveb].[Excess Y/N]"
MySQL = MySQL & "From [slq-Cnt Mnth Calc reserveb]"
MySQL = MySQL & "Group By [slq-Cnt Mnth Calc reserveb].[Fiscal Year],[slq-Cnt Mnth Calc reserveb].[Reporting Month]"
MySQL = MySQL & "Having([slq-Cnt Mnth Calc reserveb].[Fiscal Year]=2012 And [slq-Cnt Mnth Calc reserveb].[Reporting Month]= " & strInput & ")"
'Step 6: Instantiate and specify your recordset
Set MyRecordset = New ADODB.Recordset
MyRecordset.Open MySQL, CurrentProject.Connection
'Step 7: Instantiate Excel. If Excel isn't Loaded, Error # 429 occurs.
Set xl = GetObject(, "Excel.Application")
'Step 8: Open ItemIdCount.xlsx
Set xlwkbk = xl.Workbooks.Open("Q:\WWC Common\TAMPA Files\LSG Excess Calc New mdb Method .xlsx")
Set xlsheet = xlwkbk.Worksheets("data")
xl.Visible = True
xlwkbk.Windows(1).Visible = True
'Step 9: Find First empty cell and use that to build a dynamic range
Sheets("AppendData").Select
MyRange = "A" & _
ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row + 1
'Step 10: Copy the recordset to First Empty Row
ActiveSheet.Range(MyRange).CopyFromRecordset MyRecordset
ActiveWorkbook.Save
'ActiveWorkbook.Close
MsgBox "Report Has Been Updated"
'Step 11: e variables
Set xlsheet = Nothing
Set xlwkbk = Nothing
Set xlApp = Nothing
Set Db = Nothing
MyRecordset.Close
End Sub