AccessDataSource SQL With User Defined Function

I come from a MS Access background and am trying to develope a ASP.NET app using VB.  I have a mdb which I have connected to with the accessdatasource control.

How do I use my own function in an expression in the SQL select statement to add a calculated field to the query?

Basically I have a table that has latitudes and longitudes and a function which determines distance between two sets of coordinates.  I want to display all the records that are within x number of miles from a coordinate set but when I try to call my function from the accessdatasource it says its not defined.  What is the best way to do this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Is your function simple? If it is you could create a function in your SQL database then you could make your sql statement call it directly (See below).

Failing that Create 2 ADODB Recordsets. Load one with the conents from the Database. The second Recordset is created manually and will mimic it but with the addition of a field for the functions returned value. Sorry it wording might be a bit unclear but the example should work.

Might not be the best method but it is working for us.

Hope this helps
-- SQL

CREATE FUNCTION <Scalar_Function_Name, sysname, FunctionName> 
	-- Add the parameters for the function here
	<@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
RETURNS <Function_Data_Type, ,int>
	-- Declare the return variable here
	DECLARE <@ResultVar, sysname, @Result> <Function_Data_Type, ,int>

	-- Add the T-SQL statements to compute the return value here
	SELECT <@ResultVar, sysname, @Result> = <@Param1, sysname, @p1>

	-- Return the result of the function
	RETURN <@ResultVar, sysname, @Result>


-- To Call it would just be....

FROM ...

-- RecordSets Method
Dim RS1 As ADODB.Recordset
Dim RS2 As ADODB.Recordset

Set RS1 = New ADODB.Recordset
Set RS2 = New ADODB.Recordset

RS1.Open strSQL, Conx, adOpenKeyset, adLockOptimistic, adCmdText

RS2.Fields.Append "Field1", adVarChar, 50
RS2.Fields.Append "Field2", adVarChar, 50
RS2.Fields.Append "NEWFIELD", adVarChar, 50

Do While Not RS1.EOF
    RS2!Field1 = RS1!Field1
    RS2!Field2 = RS1!Field2
    RS2!NEWFIELD = MyFunction(RS1!Field2)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jet46Author Commented:
Thanks for your help.  I think I am going to give the ADO method a try.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.