How can I run a code line from Excel

  • Hi,

    How can I run a code line from Excel, without opening SQL Server 2005 and executing the command???

    Let's suppose that I want to run the following code: select * from Tbl1

    Thanks

  • In Excel There is one Option called Import External Data in the Data Tab In the Main Menu, Make use of One Data Source to Connect with the server.

  • Thanks vinuraj, but I was thinking in something more like using VBA code to run the code line in SQL and retrieve the result. Is this possible???

    Thanks

  • Yes it is, but there's far more to it than a quick post to the forums.

    But something like this should get you started.

    Private Sub yourSubroutine()

    Set con = CreateObject(“adodb.connection”)

    Set rs = CreateObject(“adodb.recordset”)

    con.Open “Provider=sqloledb; Data Source=db-serv;Initial Catalog=yourDB; User Id=admin; Password=admin;”

    rs.Open “select * from yourTable?, con

    i = 2

    Do While Not rs.EOF

    Cells(i, 2) = rs(“yourColumn”)

    rs.movenext

    i = i + 1

    Loop

    rs.Close

    Set rs = Nothing

    Set con = Nothing

    End Sub

    For more detailed info, I suggest googleing "VBA, SQL and Excel"

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Thanks Jason Selburg but there is any book where I can find what I need??

    Thank you again

  • Sure, there are a ton of them.

    Do search on amazon.com

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg

Viewing 6 posts - 1 through 5 (of 5 total)

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