469,138 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,138 developers. It's quick & easy.

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.

Thanks

Brett


Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE FUNCTION dbo.countrecordsnow (
  3. )
  4. RETURNS INT
  5. AS
  6. BEGIN
  7.  
  8. declare
  9. @v_count INT,
  10. @v_return INT
  11.  
  12. BEGIN
  13. select @v_count = COUNT(*) from dbo.ANALYST
  14.  
  15. set @v_return = @v_count
  16. return (@v_return)
  17.  
  18. end
  19.  
  20. END
  21.  
  22.  
Aug 2 '10 #1
1 2957
vb5prgrmr
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
varuns
reply views Thread by varuns | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.