Execute SQL Query from excel

user3708197

I'm a bit stuck for a while in a small project to generate results from several sql queries in several excel Sheets, I am trying to use SQL Server 2008 and it's the first time I code VBA I tried this code (for a SQL single query) but I still have compilation problems

Sub New_Feuil1() 
    ThisWorkbook.Activate 

     'First clear the contents from the query
    Worksheets("Feuil1").Select 
    Range("A2").Select 
    Do Until ActiveCell = "" 
        ActiveCell.Offset(1).Select 
    Loop 
    Range("A4", ActiveCell.Offset(-1, 3)).ClearContents 

     'Get reporting date
    ReportingDate = ThisWorkbook.Sheets("Parameters").Range("D1") 

     'Format the value for use in the SQL query
    ReportingDateFor = Format(ReportingDate, "yyyy-mm-dd") 

    Worksheets("Feuil1").Select 
    Range("A1").Select 

    Dim cnn As New ADODB.Connection 
    Dim rst As New ADODB.Recordset 
    Dim StrQuery1 As String 
    Dim ConnectionString As String 

    ConnectionString ="ODBC;" & _
    "Driver={SQL Server Native Client 10.0};" & _
    "Server=187.125.254.231;" & _
    "Database=database;" & _
    "UID=sa; PWD=pwd"
    cnn.Open ConnectionString 
    cnn.CommandTimeout = 900 

     'Queries to be executed
    StrQuery1 = StrQuery1 & "Select Id from Users" 

    rst.Open StrQuery1, cnn, adOpenForwardOnly, adLockReadOnly 
    rst.Close 

    Debug.Print "StrQuery1:"; StrQuery1 
    cnn.Close 

    ThisWorkbook.Sheets("Central Dashboard").Select 
    Sheets("Feuil1").Range("A2").CopyFromRecordset rst

End Sub 

is there any other solution ?

Krish

it seems you are new to programming :).. before you use any variables please declare them this will help you to understand them quickly.

like:

Dim ReportingDate as Date
ReportingDate = ThisWorkbook.Sheets("Parameters").Range("D1")

Dim ReportingDateFor As String
ReportingDateFor = Format$(ReportingDate, "yyyy-mm-dd")

also check your connection string. try this connection string.

ConnectionString = "Driver={SQL Server Native Client 10.0};Server=187.125.254.231;Database=database;UID=sa; PWD=pwd"

Apart from that, looking at your code you are connecting to server, opening recordset, closing recordset and finally closing the connection AND THEN trying to retrieve the results. logically this will never work :) :)

try this:

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As ADODB.Command

Dim ConnectionString As String
ConnectionString = "Driver={SQL Server Native Client 10.0};Server=187.125.254.231;Database=database;UID=sa; PWD=pwd"

cnn.Open ConnectionString

'Queries to be executed
Dim StrQuery1 As String
StrQuery1 = StrQuery1 & "Select Id from Users"

'Prepare SQL execution
cmd.Name = "SelectUsers"
cmd.ActiveConnection = conn
cmd.CommandText = StrQuery1

Set rst = cmd.Execute
If Not rst.EOF Then
    With Sheets(1).Cells ' Enter your sheet name and range here
        .ClearContents ' clears the entire sheet
        .CopyFromRecordset rst ' copy the result
    End With
Else
    MsgBox "no records found.."
End If

'After work done close connection
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python - Execute multiple SQL query from file

From Dev

How to execute SQL query from within an SQL query

From Dev

Run an SQL Query With a Parameter from Excel 2007

From Dev

Query SQL Server from Excel with parameters

From Dev

Jsch to execute a sql query

From Dev

Allow select SQL query to be execute from stored procedure only

From Dev

How to execute a spark sql query from a map function (Python)?

From Dev

How to execute SQL Dynamic query String from table row?

From Dev

Execute SQL Server Pass-Through Query From Access VBA

From Dev

Data factory: how to execute query after copy from blob to SQL?

From Dev

How to execute SQL Dynamic query String from table row?

From Dev

Can't execute query to an Azure SQL Server from PowerShell

From Dev

SQL code copy / pasted from Excel document,with no visible errors, will not execute

From Dev

Execute any Raw SQL Query

From Dev

Phalcon execute sql query in the model

From Dev

Execute SQL Query on aspx page

From Dev

sql syntax error execute query

From Dev

Safely execute a dynamic sql query

From Dev

SQL (NOT IN) Query takes forever to execute

From Dev

Execute sql query using php

From Dev

how to execute sql query in yii?

From Dev

Execute any Raw SQL Query

From Dev

Execute SQL Query on aspx page

From Dev

Execute SQL query c#

From Dev

Execute Normal SQL Query in Hibernate

From Dev

What's the best way to execute SQL Query on large excel file using vb.net?

From Dev

What's the best way to execute SQL Query on large excel file using vb.net?

From Dev

Execute SQL Query multiple times in Excel and change variable based off row

From Dev

I want to execute another SQL query on the ResultSet from the previous query using JDBC in Java Eclipse

Related Related

  1. 1

    Python - Execute multiple SQL query from file

  2. 2

    How to execute SQL query from within an SQL query

  3. 3

    Run an SQL Query With a Parameter from Excel 2007

  4. 4

    Query SQL Server from Excel with parameters

  5. 5

    Jsch to execute a sql query

  6. 6

    Allow select SQL query to be execute from stored procedure only

  7. 7

    How to execute a spark sql query from a map function (Python)?

  8. 8

    How to execute SQL Dynamic query String from table row?

  9. 9

    Execute SQL Server Pass-Through Query From Access VBA

  10. 10

    Data factory: how to execute query after copy from blob to SQL?

  11. 11

    How to execute SQL Dynamic query String from table row?

  12. 12

    Can't execute query to an Azure SQL Server from PowerShell

  13. 13

    SQL code copy / pasted from Excel document,with no visible errors, will not execute

  14. 14

    Execute any Raw SQL Query

  15. 15

    Phalcon execute sql query in the model

  16. 16

    Execute SQL Query on aspx page

  17. 17

    sql syntax error execute query

  18. 18

    Safely execute a dynamic sql query

  19. 19

    SQL (NOT IN) Query takes forever to execute

  20. 20

    Execute sql query using php

  21. 21

    how to execute sql query in yii?

  22. 22

    Execute any Raw SQL Query

  23. 23

    Execute SQL Query on aspx page

  24. 24

    Execute SQL query c#

  25. 25

    Execute Normal SQL Query in Hibernate

  26. 26

    What's the best way to execute SQL Query on large excel file using vb.net?

  27. 27

    What's the best way to execute SQL Query on large excel file using vb.net?

  28. 28

    Execute SQL Query multiple times in Excel and change variable based off row

  29. 29

    I want to execute another SQL query on the ResultSet from the previous query using JDBC in Java Eclipse

HotTag

Archive