I'm trying to run this for a work query and it works, unless I have more than 32,000 being run. I receive an overflow error. Any suggestions?
Public cn As New ADODB.Connection
Public rs As New ADODB.Recordset
Function RawData()
'Disable the screen.
Application.ScreenUpdating = False
'this conects the excell document to the access document
With cn
.ConnectionString = "C:\Users\rich.wolff\Desktop\2014POSDatabase\HMKPOSDatabase2014.accdb"
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open
End With
'then to run a query I show one i have done so you can see how it works
Dim SQL As String
Dim i As Integer
SQL = "TRANSFORM Sum(StoreSalesData.qty) AS SumOfqty"
SQL = SQL & " SELECT CensusDataBrief.IncomePerHousehold, CensusDataBrief.AverageHouseValue, StoreSalesData.DEL_DOC_NUM"
SQL = SQL & " FROM CensusDataBrief INNER JOIN (VSNConversionData INNER JOIN (([Divisional Vice President List] INNER JOIN ([District Sales Manager List] INNER JOIN (([Tempur-Pedic Reps] INNER JOIN [Sleepys Store List] ON [Tempur-Pedic Reps].[Territory Name]
= [Sleepys Store List].TPXTerritory) INNER JOIN StoreSalesData ON [Sleepys Store List].[Store Code] = StoreSalesData.STR) ON [District Sales Manager List].DSM = [Sleepys Store List].DSM) ON ([Divisional Vice President List].DVP = [Sleepys Store List].DVP)
AND ([Divisional Vice President List].DVP = [District Sales Manager List].DVP)) INNER JOIN [Market Sales Director List] ON ([Market Sales Director List].MSD = [District Sales Manager List].MSD) AND ([Market Sales Director List].MSD = [Sleepys Store List].MSD)
AND ([Divisional Vice President List].DVP = [Market Sales Director List].DVP)) ON VSNConversionData.VSN = StoreSalesData.VSN) ON CensusDataBrief.ZipCode = [Sleepys Store List].Zip"
SQL = SQL & " WHERE (((CensusDataBrief.IncomePerHousehold)=""$50,000-$99,999"") AND ((StoreSalesData.[WRTN DT]) Between #4/1/2014# And #6/30/2014#) AND ((VSNConversionData.[Item Type])=""mattress"") AND ((VSNConversionData.[Item Brand])=""Tempur-Pedic""))
OR (((CensusDataBrief.IncomePerHousehold)=""$50,000-$99,999"") AND ((StoreSalesData.[WRTN DT]) Between #4/1/2014# And #6/30/2014#) AND ((VSNConversionData.[Item Type])=""foundation"" Or (VSNConversionData.[Item Type])=""bedbase - adjustable"" Or (VSNConversionData.[Item
Type])=""Pillows""))"
SQL = SQL & " GROUP BY CensusDataBrief.IncomePerHousehold, CensusDataBrief.AverageHouseValue, StoreSalesData.DEL_DOC_NUM"
SQL = SQL & " PIVOT VSNConversionData.[Item Type];"
i = 1
With rs
.Open SQL, cn 'inputs SQL into access
Do While Not .EOF 'runs loop until no more data avalable
Cells(i, 1) = .Fields("IncomePerHousehold")
Cells(i, 2) = .Fields("AverageHouseValue")
Cells(i, 3) = .Fields("Del_Doc_Num")
Cells(i, 4) = .Fields("Mattress")
Cells(i, 5) = .Fields("Foundation")
Cells(i, 6) = .Fields("Bedbase - Adjustable")
Cells(i, 7) = .Fields("Pillows")
i = i + 1
.MoveNext 'moves to next line in access
Loop
End With
rs.Close 'closes SQL
' after you have run all the queries you need
cn.Close
'Enable the screen.
Application.ScreenUpdating = True
End Function