ADODB returning a value from sql sp (1 Viewer)

mendesj1

Registered User.
Local time
Today, 14:42
Joined
Nov 9, 2011
Messages
30
Guys i need help with returning a single value from my sql stored procedure i want to call the sp with vba and return a single value all the code is below any help would be great.

'Access ADODB CODE

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset


Dim SQLServer As String
Dim Catalog As String
Dim SQLString As String
Dim SQLInvNo As String
Dim PURCHASEORDERNO As Integer

PURCHASEORDERNO = Me.PURCHASEORDERNO


SQLServer = "Gretzky\RNYSQLAccess"
Catalog = "SQLcafe2010"
SQLString = "Exec sp_Suppliername " & PURCHASEORDERNO




'Create a new ADO Connection object
Set cn = New ADODB.Connection

'Use the Access 10 and SQL Server OLEDB providers to
'open the Connection
'You will need to replace MySQLServer with the name
'of a valid SQL Server
With cn
.Provider = "Microsoft.Access.OLEDB.10.0"
.Properties("Data Provider").Value = "SQLOLEDB"
.Properties("Data Source").Value = SQLServer
.Properties("Integrated Security").Value = "SSPI"
.Properties("Initial Catalog").Value = Catalog
.Open
End With

'Create an instance of the ADO Recordset class, and
'set its properties


Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Source = SQLString
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open

End With


__________________________
SQL SP

USE [SQLCAFE2010]
GO
/****** Object: StoredProcedure [dbo].[sp_Deltacurrentid] Script Date: 05/07/2013 07:49:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter procedure [dbo].[sp_Suppliername]
@PoNumber int


As
Begin
declare @SupplierName as nvarchar (100)

set @suppliername = (SELECT Suppliers.[SUPPLIER NAME]
FROM PurchaseOrders INNER JOIN
Suppliers ON PurchaseOrders.SUPPLIERID = Suppliers.SUPPLIERID
WHERE (PurchaseOrders.[PURCHASE ORDER NO] = @PoNumber)
)

return @PoNumber
end
 

mendesj1

Registered User.
Local time
Today, 14:42
Joined
Nov 9, 2011
Messages
30
all i want to do is set the return value from sql "return @PoNumber" to a variable in vba the result of the query is a single value
 

mdlueck

Sr. Application Developer
Local time
Today, 14:42
Joined
Jun 23, 2011
Messages
2,631
1) Set up the calling of the SP in an ADO.Command / ADO.Parameters object
2) Fire the .Execute method of the Command object
3) That returns an ADO.Recordset object with the output variables from the SP.
4) Take that ADO.Recordset object / fetch value(s) from it, close it, cleanup

Here is an example of how to use ADO.Command / ADO.Parameters objects to issue a SQL SELECT. Combine it with the example of using an ADO.Command / ADO.Parameter object to execute a Stored Procedure contained in my prior shared link:

Example of SQL SELECT using ADODB.Command and ADODB.Parameters objects to Access tables
http://www.access-programmers.co.uk/forums/showthread.php?t=230610#post1176746
 

mendesj1

Registered User.
Local time
Today, 14:42
Joined
Nov 9, 2011
Messages
30
thanks for the help but i am still not getting it to work, i can tell that the single value is getting returned to my record set from my stored procedure but have a problem setting it equal to a variable in vba
 

mdlueck

Sr. Application Developer
Local time
Today, 14:42
Joined
Jun 23, 2011
Messages
2,631
but have a problem setting it equal to a variable in vba

Show me that bit of VBA code.

Your SP syntax looks a bit different than I have success with. I either SELECT things and not redirect them to variables in the Stored Procedure, or I use variables and at the end of the Stored Procedure, I specifically SELECT the variables into virtual column names so that the results will be successfully received in VBA. Example of that:

Code:
-- Define a query to prepare the output value based on the variables which have been prepared
SELECT @totaluniqueprojectpartcount AS [totaluniqueprojectpartcount],
       @uniqueprojectpartcount AS [uniqueprojectpartcount],
       @faapvdneeded AS [faapvdneeded],
       @faordneeded AS [faordneeded]
So in this SP there are a number of different queries each performing the calculation needed for one variable. At the end of the SP then I SELECT all of the variables back into virtual field names so that I know which data to expect in which field back in the VBA code ADO.Recordset object. Better? :cool:
 

mendesj1

Registered User.
Local time
Today, 14:42
Joined
Nov 9, 2011
Messages
30
the sp just runs a simple query set the result of the query to a variable (single result value) the returns the value, it gets down to .open in the vba code then i get a run time erro conversion failed when converting the nvarchar value (the result of the querry a -text-) to data type int , i am not sure why its trying to make it an int

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset


Dim SQLServer As String
Dim Catalog As String
Dim SQLString As String
Dim SQLInvNo As String
Dim PURCHASEORDERNO As Integer

PURCHASEORDERNO = Me.PURCHASEORDERNO


SQLServer = "Gretzky\RNYSQLAccess"
Catalog = "SQLcafe2010"
SQLString = "Exec sp_Suppliername " & PURCHASEORDERNO




'Create a new ADO Connection object
Set cn = New ADODB.Connection

'Use the Access 10 and SQL Server OLEDB providers to
'open the Connection
'You will need to replace MySQLServer with the name
'of a valid SQL Server
With cn
.Provider = "Microsoft.Access.OLEDB.10.0"
.Properties("Data Provider").Value = "SQLOLEDB"
.Properties("Data Source").Value = SQLServer
.Properties("Integrated Security").Value = "SSPI"
.Properties("Initial Catalog").Value = Catalog
.Open
End With

'Create an instance of the ADO Recordset class, and
'set its properties


Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Source = SQLString
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open

End With
 

mdlueck

Sr. Application Developer
Local time
Today, 14:42
Joined
Jun 23, 2011
Messages
2,631
Code:
With cn
      .Provider = "Microsoft.Access.OLEDB.10.0"
      .Properties("Data Provider").Value = "SQLOLEDB"
      .Properties("Data Source").Value = SQLServer
      .Properties("Integrated Security").Value = "SSPI"
      .Properties("Initial Catalog").Value = Catalog
      [B][COLOR=Red].Open[/COLOR][/B]
   End With

This will not do what you are seeking.

From my example:

Code:
   [B][COLOR=Red]Set adoRS = .Execute()[/COLOR][/B]
  End With

  With adoRS
    'Was no record found?
    If .BOF Or .EOF Then
      Me.Clear
      LocateByPartNumber = False
    Else
      'Fetch the value found
      Me.aid = Nz(adoRS.Fields.Item("aid"), 0)
      Me.title = Nz(adoRS.Fields.Item("title"), vbNullString)
      Me.qtyper = Nz(adoRS.Fields.Item("qtyper"), 0)
      Me.oldqtyper = Nz(adoRS.Fields.Item("oldqtyper"), 0)
      Me.addpartrecordflg = Nz(adoRS.Fields.Item("addpartrecordflg"), False)
      Me.doneflg = Nz(adoRS.Fields.Item("doneflg"), False)
      LocateByPartNumber = True
    End If

    'Close the database table
    .Close
  End With

  'Clean up the connection to the database
  Set adoRS = Nothing
  Set adoCMD = Nothing
 

Users who are viewing this thread

Top Bottom