Solved

Stored procedure equivalent of ADODB query/recordset

Posted on 2003-11-12
2
280 Views
Last Modified: 2006-11-17
I want to create a stored procedure to replace the following VBA in an Access form:


**********************************************

Dim cn as ADODB.Connection
Set cn = CurrentProject.Connection

Dim rs as ADODB.Recordset
Set rs = New ADODB.Recordset

Dim SQL
SQL = "SELECT col1, col2, col3  FROM [table] WHERE col0 = [somevalue]"

rs.Open SQL, cn, 1,3,1

MsgBox ("Column 1 data is: " & rs("col1"))

**********************************************


I also need to know how to access the stored procedure via VBA.  Can someone please take the above example and (1) convert the code so it's valid in a stored procedure; and (2) provide an example of the VBA code which will call this stored procedure and assign values to variables based on what is returned.

Thanks!
0
Comment
Question by:FunkiNATEr
2 Comments
 
LVL 3

Expert Comment

by:monvelasquez
ID: 9738029
in SQL

--[CODE]-------------------------------------------------------
Create Procedure sp_QueryData
AS
   SELECT col1, col2, col3  FROM [table] WHERE col0 = somevalue
--[/CODE]-------------------------------------------------------

in VB

--[CODE]-------------------------------------------------------
Dim cn as ADODB.Connection
Set cn = CurrentProject.Connection

Dim rs as ADODB.Recordset
Set rs = New ADODB.Recordset

Dim SQL
SQL = "execute sp_QueryData"

rs.Open SQL, cn, 1,3,1

MsgBox ("Column 1 data is: " & rs("col1"))

--[/CODE]------------------------------------------------------
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 9740532
I would modify somewhat the otherwise excellent suggestion by monvelasquez.

I suspect that [somevalue] is a variable in which case make the following changes to the stored procedure:

Create Procedure sp_QueryData (@SomeValue varchar(50)) --Change to some other character data type as appropriate.

AS
   SELECT col1, col2, col3  FROM [table] WHERE col0 = @SomeValue

And the VBA code as follows:

SQL = "execute sp_QueryData 'somecharactervalue'"

Here I am making the assumption that it is character data, if it is numeric than make the following changes:

Create Procedure sp_QueryData (@SomeValue int) --Change to some other numeric data type as appropriate.

AS
   SELECT col1, col2, col3  FROM [table] WHERE col0 = @SomeValue

And the VBA code as follows:

SQL = "execute sp_QueryData somenumericvalue"

Anthony
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL SERVER - Index skipped a number 2 27
xml files 7 29
Stored Procedure needs owner to execute 5 21
MS SQL + date 6 23
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question