SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Access ADODB call to SQL function


Access ADODB call to SQL function

Author
Message
a_ud
a_ud
SSC-Enthusiastic
SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)

Group: General Forum Members
Points: 166 Visits: 336
Help needed here!

I've got a system made out of: Access front-end + SQL Server back-end (typical).

I need to call from Access VBA a function in SQL Server, but I can't get it to run, some detail is missing. Say the SQL function is called XFunc(@id INT), so it has one parameter @id.

The ADODB code then would look sth like:

Dim cmd as NEW adodb.command
cmd.activeconnection = <the connexion you've set up>
cmd.CommandText = "XFunc"
cmd.Parameters = ??????????

cmd.Execute


I've tried this but VBA throws an error, either I'm missing the parameter @id when calling XFunc, or there is a specific ADODB command to call function (and maybe I'm using the stored procedures one).

What am I missing here? Are there any good sources of ADODB documentation you can recommend?

Thanks in advance, A.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25817 Visits: 17509
a_ud (3/13/2013)
Help needed here!

I'm got a system made out of: Access front-end + SQL Server back-end (typical).

I need to call from Access VBA a function in SQL Server, but I can't get it to run, some detail is missing. Say the SQL function is called XFunc(@id INT), so it has one parameter @id.

The code then would look sth like:

Dim cmd as NEW adodb.command
cmd.activeconnection = <the connexion you've set up>
cmd.CommandText = "XFunc"
cmd.Parameters = ??????????

cmd.Execute


I've tried this but VBA throws an error, either I'm missing the parameter @id when calling XFunc, or there is a specific ADODB command to call function (and maybe I'm using the stored procedures one).

What am I missing here? Are there any good sources of ADODB documentation you can recommend?

Thanks in advance, A.


That isn't quite how sql functions work.

Something like this should work.

select dbo.XFunc(@Parm)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
a_ud
a_ud
SSC-Enthusiastic
SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)SSC-Enthusiastic (166 reputation)

Group: General Forum Members
Points: 166 Visits: 336
Hi Sean,

Thanks, that helps, but could you complete it?

So let's say you've got a string var 'X' and want to return the function result there. Would this be the code for that?

dim X as String
X = Docmd.RunSQL "select " & dbo.XFunc(@Parm)

Thanks in advance, A.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25817 Visits: 17509
I don't remember all of the syntax off the top of my head and I haven't used this in several years. I will do my best but google will likely fill in the holes and syntax issues. This is reasonably close IIRC.

Let's say you have a scalar function called MyFunction that receives a single parameter.


sql = "select dbo.MyFunction(?)"

dim myParam as new ADODB.Parameter
myParam.Value = [The value to pass in]

command.Parameters.Append myParam

dim rs as new ADODB.RecordSet
set rs = command.Execute()



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search