update returns invalid object
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Update returns "Invalid object name"

Update returns "Invalid object name"

resolvedResolved · Low Priority · Version 2003

replyReply Wed 30 Mar 2011, 15:35Delegate Babawande said...

Babawande has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

Update returns "Invalid object name"

Hi

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.

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”.




Sub CustomerExtract()

Dim ws As Worksheet

Set ws = Worksheets("Sheet1")

Worksheets("Sheet").Range("A2:IV65536").ClearContents


sSQLDB = "CReport"
mstrOLEDBConnect = "Provider=SQLOLEDB.1;" & _
"Data Source=NNN-XXX-30;" & _
"Initial Catalog=" & sSQLDB & ";" & _
"Integrated Security=SSPI"

Set conx = New ADODB.Connection
conx.ConnectionString = mstrOLEDBConnect
conx.ConnectionTimeTime = 0
conx.CommandTimeTime = 0
conx.Open


myData = "CREATE Table #AmountCount1 (CustomerID int, CustomerType varchar(15), CustomerName varchar(1000),SName varchar(8), AmountCount int) "
myData = "INSERT INTO #AmountCount1 (CustomerID, CustomerType, CustomerName, SName, AmountCount) "
myData = "SELECT CustomerID, CustomerType, CustomerName, SName, COUNT(SName) AS AmountCount "
myData = myData & "FROM dbo.vw_KPIAmount "
myData = myData & "GROUP BY CustomerID, CustomerType, CustomerName, SName "


myData = "CREATE TABLE #AmountCount2 (CustomerID int, CustomerType varchar(15), CustomerName varchar(1000), Time1 int, Time2 int, Time4 int) "
myData = "INSERT INTO #AmountCount2 (CustomerID, CustomerType, CustomerName) "
myData = "SELECT DISTINCT CustomerID, CustomerType, CustomerName FROM dbo.vw_KPIAmount "

Set Rst = conx.Execute(myData)

Worksheets("Sheet1").Range("A2").CopyFromRecordset Rst


myData1 = "UPDATE #AmountCount2 "
myData1 = myData1 & " SET Time1 = AmountCount "
myData1 = myData1 & " FROM #AmountCount2 a, #AmountCount1 b "
myData1 = myData1 & " WHERE a.CustomerID = b.CustomerID "
myData1 = myData1 & " AND SName = 'Time1' "


Set Rst = conx.Execute(myData1)

Worksheets("Sheet1").Range("g2").CopyFromRecordset Rst


myData1 = "SELECT Time1 "
myData1 = myData1 & "FROM #AmountCount2"
myData1 = myData1 & "ORDER BY CustomerID"

myData1 = myData1 & "DROP TABLE #AmountCount1"
myData1 = myData1 & "DROP TABLE #AmountCount2"

End Sub

For upcoming training course dates see: Pricing & availability

replyReply Mon 9 May 2011, 11:16Trainer Jacob said...

RE: Update returns "Invalid object name"

Hi Babawande

I have emailed you about this post, hope this helps.

Regards

JAcob

Mon 16 May 2011: Automatically marked as resolved.

 

Please browse our web site to find out more about
excel visual basic training and other Microsoft training courses.

Excel tip:

Activating the formula bar with the keyboard in Excel 2010

The formula bar in Excel 2010 contains a number of different formulas. However, it is time consuming to open the formula bar every time and insert the formula using the mouse. Instead, you can activate the formula bar with the keyboard. Simply press the F2 button on the keyboard then the information in the cell will expand allowing you to edit the information in the cell.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard