0

It is possible to call window sql database user-defined function using access vba code?

I found a function call db.testFunction(id) in the sql database; I use SQLNCL11.1 as the provider and I use ADODB for the connection.

How do I use this function in access vba code?

2

1 Answer 1

1

Assuming that this application has working linked tables?

Then just create a pass-though query. Type in your functon with a select statement in front. Run the pt query. It should return your value. So, in the query builder, creaate that PT query. For the source of the PT query you can type in any valid t-sql, but in this case, it makes sense to type in a test of the function.

Say this:

SELECT dbo.textFunction(12)

If that works (and MAKE SURE you get it working - you not written one line of code). Once you have that PT query working?

Then you can use this VBA code:

Sub Test33434()

   Dim lngID           As Long
   Dim lngReturnValue  As Long
   
   lngID = 1234
   
   With CurrentDb.QueryDefs("qryPass")
      .SQL = "select dbo.testFunction(" & lngID & ")"
      lngReturnValue = .OpenRecordset()(0)
   End With

End Sub

So, in above we have the value we pass, and the return value. And note how we don't have to mess with any connection object in code - so using a saved PT query is nice, since you don't have connection strings in code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.