Home » excel » excel vba – VBA ADO 'Invalid Object Name' Error; No Error SQL Server Management Studio

excel vba – VBA ADO 'Invalid Object Name' Error; No Error SQL Server Management Studio

Posted by: admin May 14, 2020 Leave a comment

Questions:

I created a function in SQL Server 2008.

I can run both of these statements in SSMS without errors:

SELECT * FROM myFunction(myParam1,'myParam2');
SELECT * FROM dbo.myFunction(myParam,'myParam2');

But this in VBA gives a ‘Invalid Object’ error on ‘myFunction’ when executing the query:

Set db = New adodb.Connection
db.Open "Provider=SQLNCLI10;Data Source=aaaaa;Initial Catalog=db_ferm;Trusted_Connection=yes;"

Sql = "SELECT * FROM myFunction(" & theParam & ",'" & theParam2 & "') "
Set GetReport = db.Execute(Sql)

These ‘Invalid Object Name’ errors suddenly started showing up today. Even now in SSMS, I will get them occasionally and have to reset the Intellisense Cache.

Ideas?

How to&Answers:

Rookie SMSS mistake!

When I created the function in SMSS, I had ‘master’ selected in the database dropdown menu (not sure how that happened) and all my SMSS query testing against the function was done in ‘master’. My ADO connection string was to another database. So it couldn’t find the function.