472,133 Members | 1,183 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

Calling a SQL Server Function From Excel VBA

Dear Forum,

As I'm new to VBA, I was hoping you could answer a VBA / SQL Server 2005 Function question for me.

The scenario is this:

I am trying to call a SQL Server 2005 Function from within Excel (as a Function).
So basically, I want my excel user to type into a cell =MyFunction() which will call the SQL Sever 2005 Function.

I have managed to call Stored Procs etc from within Excel VBA, but functions remain a mystery to me. I just cannot seem to assign the returned value correctly.

To further explain, I have created a small sample Function in SQL Server and is posting that code below.



Expand|Select|Wrap|Line Numbers
  2. CREATE FUNCTION dbo.countrecordsnow (
  3. )
  5. AS
  6. BEGIN
  8. declare
  9. @v_count INT,
  10. @v_return INT
  12. BEGIN
  13. select @v_count = COUNT(*) from dbo.ANALYST
  15. set @v_return = @v_count
  16. return (@v_return)
  18. end
  20. END
Aug 2 '10 #1
1 3461
305 Expert 100+
use a recordset object to execute your stored procedure and since you have not aliased the count, you would use rs.fields(0)

Good Luck
Aug 3 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Satish Chimakurthi | last post: by
3 posts views Thread by Ken | last post: by
1 post views Thread by kumar yogesh via .NET 247 | last post: by
reply views Thread by Todd Lu | last post: by
13 posts views Thread by ukrbend | last post: by
reply views Thread by varuns | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.