Results 1 to 7 of 7

Thread: How to capture result from stored procedure

  1. #1
    Join Date
    Nov 2005
    Posts
    3

    How to capture result from stored procedure

    I am using Access with SQL backend. I have a stored proc in sql that returns an int. I want to capture this value and use it in my code. How do I do this?

  2. #2
    Join Date
    Jan 2003
    Location
    UK
    Posts
    277
    you can do it like the example below but there may be better ways...

    [vb]
    Dim qdf as QueryDef
    Dim rst as recordset
    Dim wrkODBC As Workspace, cn As Connection, dbs as Database

    Set dbs = CurrentDb

    Set wrkODBC = CreateWorkspace("", "sa", "", dbUseODBC)
    Set cn = wrkODBC.OpenConnection("NewConnection", dbDriverNoPrompt, False, "ODBC;DATABASE=MyDB;UID=MyUID;PWD=MyPswd;DSN=MyDSN ")

    Set qdf = cn.CreateQueryDef("q1")

    qdf.SQL = "{ call usp_MyStoredProc }"
    Set rst = qdf.OpenRecordset(dbOpenSnapshot)
    [/vb]

    the recordset will be returning your integer

  3. #3
    Join Date
    Nov 2005
    Posts
    3
    if result set returns a singe integer value, can I just declare an int variable and store result in it?

  4. #4
    Join Date
    Jan 2003
    Location
    UK
    Posts
    277
    yes absolutely...

    Dim MyInteger as Integer

    MyInteger = rst.Fields(0)

  5. #5
    Join Date
    Nov 2005
    Posts
    3
    i do
    dim synsetno as integer
    Set db = CurrentDb
    Set qdflist = db.QueryDefs("runstoredproc") 'passthroughquery
    strsql = <some string>
    qdflist.SQL = strsql
    DoCmd.SetWarnings (False)
    DoCmd.OpenQuery "runstoredproc"

    which works

    when i try
    qdflist.openrecordset instead of docmd.openquery
    synsetno = qdflist.Fields(0)

    It tells me field not found. The stored proc ran ok and I get a datasheetview of the result integer returned. How do I store the result in a variable. I tried your method also, but to no avail.

  6. #6
    Join Date
    Jan 2003
    Location
    UK
    Posts
    277

  7. #7
    Join Date
    Dec 2002
    Posts
    5
    Maybe try this (I bolded my modifications).

    dim rs As recordset
    dim synsetno as integer
    Set db = CurrentDb
    Set qdflist = db.QueryDefs("runstoredproc") 'passthroughquery
    strsql = <some string>
    qdflist.SQL = strsql
    Set rs = qdflist.openrecordset
    synsetno = rs.Fields(0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •