VBA update returns "Invalid Object Name"

Phresh10

New Member
Joined
Mar 10, 2010
Messages
29
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
I will appreciate a help with what's missing in this code.

I am trying to extract data from a database using VBA, then create 2 temporary tables (#AmountCount1 and #AmountCount2), after which I want to update one #AmountCount2. <o:p></o:p>
<o:p> </o:p>
The extraction works fine, I have been able to extract CustomerID, CustomerType, CustomerName and insert into the Excel sheet1, however, I can not update temporary table (#AmountCount2). The Update code returns “invalid object name”.<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
Sub CustomerExtract()<o:p></o:p>
<o:p> </o:p>
Dim ws As Worksheet<o:p></o:p>
<o:p> </o:p>
Set ws = Worksheets("Sheet1")<o:p></o:p>
<o:p> </o:p>
Worksheets("Sheet").Range("A2:IV65536").ClearContents<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
sSQLDB = "CReport"<o:p></o:p>
mstrOLEDBConnect = "Provider=SQLOLEDB.1;" & _<o:p></o:p>
"Data Source=NNN-XXX-30;" & _<o:p></o:p>
"Initial Catalog=" & sSQLDB & ";" & _<o:p></o:p>
"Integrated Security=SSPI"<o:p></o:p>
<o:p> </o:p>
Set conx = New ADODB.Connection<o:p></o:p>
conx.ConnectionString = mstrOLEDBConnect<o:p></o:p>
conx.ConnectionTimeTime = 0<o:p></o:p>
conx.CommandTimeTime = 0<o:p></o:p>
conx.Open<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
myData = "CREATE Table #AmountCount1 (CustomerID int, CustomerType varchar(15), CustomerName varchar(1000),SName varchar(8), AmountCount int) "<o:p></o:p>
myData = "INSERT INTO #AmountCount1 (CustomerID, CustomerType, CustomerName, SName, AmountCount) "<o:p></o:p>
myData = "SELECT CustomerID, CustomerType, CustomerName, SName, COUNT(SName) AS AmountCount "<o:p></o:p>
myData = myData & "FROM dbo.vw_KPIAmount "<o:p></o:p>
myData = myData & "GROUP BY CustomerID, CustomerType, CustomerName, SName "<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
myData = "CREATE TABLE #AmountCount2 (CustomerID int, CustomerType varchar(15), CustomerName varchar(1000), Time1 int, Time2 int, Time4 int) "<o:p></o:p>
myData = "INSERT INTO #AmountCount2 (CustomerID, CustomerType, CustomerName) "<o:p></o:p>
myData = "SELECT DISTINCT CustomerID, CustomerType, CustomerName FROM dbo.vw_KPIAmount "<o:p></o:p>
<o:p> </o:p>
Set Rst = conx.Execute(myData)<o:p></o:p>
<o:p> </o:p>
Worksheets("Sheet1").Range("A2").CopyFromRecordset Rst<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
myData1 = "UPDATE #AmountCount2 "<o:p></o:p>
myData1 = myData1 & " SET Time1 = AmountCount "<o:p></o:p>
myData1 = myData1 & " FROM #AmountCount2 a, #AmountCount1 b "<o:p></o:p>
myData1 = myData1 & " WHERE a.CustomerID = b.CustomerID "<o:p></o:p>
myData1 = myData1 & " AND SName = 'Time1' "<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Set Rst = conx.Execute(myData1)<o:p></o:p>
<o:p> </o:p>
Worksheets("Sheet1").Range("g2").CopyFromRecordset Rst<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
myData1 = "SELECT Time1 "<o:p></o:p>
myData1 = myData1 & "FROM #AmountCount2"<o:p></o:p>
myData1 = myData1 & "ORDER BY CustomerID"<o:p></o:p>
<o:p> </o:p>
myData1 = myData1 & "DROP TABLE #AmountCount1"<o:p></o:p>
myData1 = myData1 & "DROP TABLE #AmountCount2"<o:p></o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top