473,385 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Calling a scalar value sql server function from ADP?

9
How can I call a user-defined function that returns a string from ADP? I'd like to 'save' the returning value into a string variable.
Dec 13 '07 #1
3 15015
Jim Doherty
897 Expert 512MB
How can I call a user-defined function that returns a string from ADP? I'd like to 'save' the returning value into a string variable.

Embed it within your SQL query if that is what are working with....for instance imaginary UDF embeded within an imaginary sql statement thus returning a column namely the bolded bits shown in the function. he return value depending on the parameter passed to it in @vehicle (passed via the value contained in vehicle field)

Expand|Select|Wrap|Line Numbers
  1. SELECT FirstName,Surname,Vehicle, dbo.UDF_TransportComment(Vehicle) AS Comment From MyTable
If the above dataset was a recordsource for a form for instance, then you would grab the value from the forms textbox servicing the Comment field in much the same way as done normally ie

Expand|Select|Wrap|Line Numbers
  1.  Dim strComment as String 
  2. strComment=Me!Comment
  3.  

Server side user defined function

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE FUNCTION dbo.UDF_TransportComment
  3. ( @Vehicle varchar(100) )
  4. RETURNS varchar(255)
  5. AS
  6. BEGIN
  7. RETURN CASE 
  8.          WHEN @Vehicle IS NULL THEN 'Not known'
  9. WHEN @Vehicle ='Cycle' THEN 'Likes to Keep fit'
  10. WHEN @Vehicle ='Harley Davidson' THEN 'Super cool dude'
  11. WHEN @Vehicle ='Mono cycle' THEN 'Even cooler!'
  12.      END
  13. END
  14.  

Jim :)
Dec 13 '07 #2
sara4
9
What if the select was not a recordsource?

What if I want ot get the returning value in a variable?

I'd like to able to do sth like:

Dim strSomeVariable as String
strSomeVariable = "select [dbo].[fx_GetCode] (Me.ID_COL.Value)"


Here's UDF function:
ALTER FUNCTION [dbo].[fx_GetCode]
(
@Param1 varchar(8)
)
RETURNS varchar(6)
AS
BEGIN
DECLARE @Code as varchar(6)

-- Add the T-SQL statements to compute the return value here
SELECT @Code = CODE_COL
from T_TABLE
WHERE ID_COL = @Param1

-- Return the result of the function
RETURN @Code

END
Feb 14 '08 #3
Jim Doherty
897 Expert 512MB
Hi Sara,

You wouldn't need necessarily need an SQL Server user defined function to return this, an output parameter in a stored procedure could return what you require in this circumstance.

If all you are asking is for is a single return value to be assigned to a variable in the client application then passing in your @Param1 parameter via the command object to a stored procedure and returning the variable @Code as an Output parameter to your 'Access' application would give you your required result.

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE PROCEDURE dbo.usp_GetCode
  3. (@Param1 int,@Code varchar(8) Output)
  4. SELECT @Code = CODE_COL
  5.      from T_TABLE
  6. WHERE ID_COL = @Param1 
  7. - Return the result in the output parameter 
  8. SELECT @Code
  9. GO
  10.  
This is how you might then call it from your code in Access
in line with your posted variable

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim cmd As ADODB.Command
  3.     Dim par As ADODB.Parameter    
  4.     Dim strSomeVariable as String
  5.     Set cmd = New ADODB.Command
  6.     cmd.ActiveConnection = CurrentProject.Connection
  7.     cmd.CommandText = "dbo.usp_GetCode"
  8.     cmd.CommandType = adCmdStoredProc
  9.  
  10.     'Define the input and output variables and append them to the collection    
  11.     Set par = cmd.CreateParameter("@Param1", adVarChar, adParamInput, 8)
  12.     cmd.Parameters.Append par
  13.     Set par = cmd.CreateParameter("@Code", adInteger, adParamOutput)
  14.     cmd.Parameters.Append par
  15.  
  16. 'we can assign the value for the input variable to be passed to SQL Server by reference to an access screen controls currently displayed value for instance. 
  17.  
  18. cmd.Parameters("@Param1") = Forms!frmMyFormName!MyControlName
  19.  
  20.     cmd.Execute
  21.  
  22. 'and then assign the stored procedure return value to the access variable
  23.  
  24.     strSomeVariable = cmd.Parameters("@Code").Value
  25.  
As an aside ...user defined functions certainly have their place but are not visually exposed within the graphical user interface (GUI) of an Access ADP file whereas stored procedures are, you can see, and edit the relevant syntax.

Trust this helps you,

Regards

Jim :)
Feb 14 '08 #4

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

Similar topics

4
by: Mario Pranjic | last post by:
Hi! I have a scalar function that returns integer: xview (int) Now, I'm trying to build a procedure that has the following select inside: select atr1, xview(atr2) from tablename
1
by: Joel Thornton | last post by:
Is it possible to call a user-defined function without prefixing it with 'dbo.' within a SELECT clause somehow? Just curious; it's not a big issue but just a stylistic one for me. Thanks! ...
7
by: Steve Jorgensen | last post by:
Hi all, I've been using scalar functions as a way to perform some complex data transformation operations, and I've noticed that scalar functions reaaaaalllllyyyy sloooowwwwww thiiiiiings...
7
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop...
8
by: Bill Ehrreich | last post by:
I'm faced with a situation where I will need to calculate a column for a resultset by calling a component written as a VB6 DLL, passing parameters from the resultset to the component and setting...
5
by: Zlatko Matić | last post by:
Hello. How can I call some functions on MSDE when working in .mdb ? Especially in-line functions which are similar to stored procedures. How can I use MSDE in-line functions as recordsource for...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Roger | last post by:
I put this in the microsoft.public.dotnet.langueages.vb.data newsgroup, but noticed there aren't alot of people there frequently like this newsgroup. So I thought I would try here. I get an...
1
by: dtalas | last post by:
Hi all I'm having a problem where calling scalar stored procedures always returns NULL. I have the simple test stored procedure, that returns a single integer: ALTER PROCEDURE dbo.Test AS
0
by: Rayne | last post by:
I've got some scalar functions defined in sql server 2005. In VB.NET they are set up as stored procedures that return a single value in my tableadapter. When I preview data on the query in design...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.