Run SP from Access 2000 vba

  • Hi

    I'm sure this is easy, but it is new to me. 

    I want to pass a variable to a stored procedure from Access.  The sp is something like:

    CREATE PROCEDURE ProcedureName (@Variable varchar(20))

    AS

    INSERT INTO Table

      SELECT Field1, Field2

      FROM Table2

      WHERE Field1 = @Variable

     

    In Access, I want the content of a field to provide the value of the variable executed on SQL Server 2000.  I have the connection string and I can execute the sp fine.  I just don't know the syntax to pass the variable.

     

    Can anybody please help

    Paul

  • hi paul,

    try this code

    dim sql as string

        sql = "{call ProcedureName(?)}"

        Set qdf = SQLLink.CreateQueryDef("", sql)

        With qdf

            .Parameters("@Variable") = TxtVariable

            .Execute

            .Close

        End With

    where Procedure name is the stored procedure and TxtVariable is the value to pass and SQLLink is connecting database to SQL. qdf is query defenition

    paco

  • Here is another option using ADO

    Dim cnn AS ADODB.Connection

    Dim cmd as ADODB.Command

    cnn.ConnectionString =""

    cnn.open

    with cmd

         .ActiveConnection = cnn

         .CommandType = adCmdStoredProc

         .CommandText = "ProcedureName"

         'Issue a refresh to get the named parameters

         .Parameters.Refresh

         .Parameters("@Variable") = txtVariable

         .Execute

    End With

  • Thanks to both of you. 

    Excellent solutions.  I found the ADO worked best for me for this particular problem and can now do what I needed to do.

    This is much appreciated.

    Many thanks again,

    Paul

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply