PC Review


Reply
Thread Tools Rate Thread

Create user-defined function using Querytables

 
 
New Member
Join Date: Sep 2006
Posts: 1
 
      19th Sep 2006
Hi All,

I am working on creating a user-defined function to lookup the price from SQLserver by passing in a product code. I use the Querytables but not the ODBC add-in because it is difficult to ask every user to install in their workstations.

The name of the function is "f_checkprice", the syntax is "= f_checkprice(product)", following is the content of the function:
===================================================

Function f_checkprice(v_product)

Dim sConn As String
Dim sSql As String
Dim oQt As QueryTable

sConn = "ODBC;DRIVER=SQL Server;SERVER=SERVERXYZ;UID=;APP=Microsoft® Query;WSID=;DATABASE=DB_PRICE;Trusted_Connection=Y"

sSql = "exec usp_checkprice " & Trim(UCase(v_product)) & "'"

here = ActiveCell.Address
mycolumn = Mid(here, InStr(2, here, "$") + 1, 100)

With ActiveSheet.QueryTables.Add( _
Connection:=sConn, _
Destination:=Range("C" & mycolumn), _
Sql:=sSql)
.RefreshStyle = xlOverwriteCells
.Refresh = True
End With

End Function

===================================================

The function call a stored procedures in SQLserver, following is the stored procedures:
===================================================

Create procedure usp_checkprice

@PROD as varchar(30)

as

select price from tb_price_master
where product = @PROD

go

===================================================

Here is the problem I have now:
1. The result is directed to column C, however the result always occur 2 cells. The stored procedures actually just return 1 value but don't understand why in excel there is always a blank value come with the price

2. I put the product code in column A, then refer to it in the function, ie "=f_checkprice(A1)", then drag and fill the rows. However, the remain rows does not refresh with the lookup value

Could you please share if you have any idea about how to create such a function?

Regards,
David
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create User Defined Function with VB.Net Troy Microsoft Excel Programming 7 17th Aug 2009 06:58 PM
Create a user-defined function Max Microsoft Excel Programming 2 23rd Jan 2008 05:57 PM
How can I create a user defined function in excel? =?Utf-8?B?TWFydGluag==?= Microsoft Excel Misc 4 20th Aug 2005 06:11 PM
How to create User Defined Function Warwick Renshaw Microsoft Excel Programming 0 25th Jul 2003 07:15 AM
Re: Create help for user-defined function Tom Ogilvy Microsoft Excel Programming 0 12th Jul 2003 06:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 PM.