Results 1 to 9 of 9

Thread: Excel to execute stored procedure

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Anyone know the code to run a stored procedure in VBA or VB and populate Excel with the results -- I would take VB as well, but prefer VBA

  2. #2
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    I know VBA. I don't understand what you are trying to do. What do you mean by a stored procedure?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    How could I run a query or stored procedure from SQL in Excel. A stored procedure sits on the table in SQL ( a group of queries, essentially) and can be scheduled to execute manually or automatically.

  4. #4
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    That I don't know. Any ideas, Yonatan?

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    I have to go to a meeting right now, but when I come back I will search for the codes.

    Off hand you have to use the command Execute with Parameters.
    Chemically Formulated As:
    Dr. Nitro

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello Bebe!

    Try this and give me an update!
    Code:
    Private conn As Connection
    Private cmd As Command
    Sub p_Stored_Procedure()
      'PURPOSE: Open the Connection
      Set conn = CreateObject("ADODB.Connection")
      conn.open "dsn=dsn;uid=username;pwd=password"
      
      'PURPOSE: Call Stored Procedure
      Set cmd = CreateObject("ADODB.Command")
      Set cmd.activeconnection = conn
      cmd.commandtext = "Procedure_name"
      cmd.commandtype = adcmdStoredProc
      cmd.Parameters.Refresh
      cmd.Parameters(1).Value = "Value1"
      cmd.Parameters(2).Value = "Value2"
      cmd.Parameters(n).Value = "Valuen"
      
      'PURPOSE: Get the results from the Stored Procedure
      Dim varReturn As Variant
      cmd.Execute
      varReturn = cmd.Parameters(0).Value
      
      'PURPOSE: Get the results set from Stored Procedure
      Dim rs As Recordset
      Set rs = cmd.Execute
      conn.Close
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  7. #7
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    After you get the above code to work. I will be happy to help you populate the Result Sets into Excel. One thing at a time, otherwise you might get confuse.
    Chemically Formulated As:
    Dr. Nitro

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Location
    bebenia, PA, USA
    Posts
    241
    Wow, thank you this is a great start. I need to get the stored procedures done and I will take you up on your offer. What I have is a formatted type report in Excel -- with colors and stuff. Does than make it any more difficult. No, charts though!!!

    Thanks again!!!

  9. #9
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Hello Bebe,

    I recommend you do a small test stored procedure
    first. The more complicated the stored procedure, the
    more difficult to understand where the problem.

    Once your stored procedure runs fine, then test the
    vb code against the stored procedure.

    I have been doing Excel VBA for a while so that should
    be the easy part.

    Good Luck!

    To populate the spreadsheet, do either a CreateObject
    or GetObject against Excel.
    Chemically Formulated As:
    Dr. Nitro

Posting Permissions

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



Click Here to Expand Forum to Full Width