Import resultset from select query in .sql file to excel using VBA

Skorben

New Member
Joined
Feb 15, 2015
Messages
2
Hello Excel Experts,

First time poster here - long time lurker, but this is about to change.

I am having an issue with the following VBA code, which is supposed to do the following:
1) Connect to SQL Server
2) Use .sql file on network-drive to extract the desired select statement (sql-code)
3) Open recordset
4) Copy results to cells in excel-sheet

Code:
Sub sql_execute()

    'Declare variables'
        Set Cnn = New ADODB.Connection
        Set Rst = New ADODB.Recordset
        Dim SqlStatement As String
        Dim ConnectionString As String

ReportDateVariable = Left(Sheets("Sheet1").Cells(2, 2), 10) & " 00:00:00.000"

'Connection to SQl Server
ConnectionString = "Provider=SQLOLEDB.1;" & _
"Integrated Security=SSPI;" & _
"Initial Catalog=DB;" & _
"Data Source=Server"

Cnn.Open ConnectionString
Cnn.CommandTimeout = 900
 
SqlTextFile = "PathTo.sqlFile"
Debug.Print SqlTextFile
Dim hFile As Long
hFile = FreeFile
Open SqlTextFile For Input As #hFile
SqlStatement = Input$(LOF(hFile), hFile)
SqlStatement = Replace(SqlStatement, "ReportDateVariable", ReportDateVariable)
Close #hFile
Debug.Print SqlStatement
 
 
Rst.Open SqlStatement, Cnn
 

If Rst.State = adStateClosed Then
    MsgBox "Recordset is closed"
Else
    MsgBox "Recordset is open"
End If

'Copy the results to cell A6 in "Sheet1"
Set WSP1 = Sheets("Sheet1")
WSP1.Activate
If Rst.EOF = False Then WSP1.Cells(6, 1).CopyFromRecordset Rst

Rst.Close
Set Rst = Nothing

Cnn.Close
Set Cnn = Nothing

End Sub

Issue:
For some reason the recordset will not open. The msgbox always returns that the state of the recordset is closed. The error occurs at "If Rst.EOR = False Then".
The SQL-code is a regular select-statement. I have used this .sql-file before and succesfully achieved the above without any error (unfortunately I forgot to save the code), but for some reason after trying to replicate it I am getting this error.
Changing the variable "SqlStatement" to a "SELECT * FROM TABLE1" and the code works...

I am going out of my mind! You know the feeling...

I hope my description of the issue makes sense.
If not, please do not hesitate to ask.

Thanks in advance,

Skorben
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Solved it!
For some reason the string in the variable was not acting correctly in terms of spacing.
Fixed some of the SQL-code and it worked.

If anyone at some point runs into the same problem, please do not hesitate to PM me.
 
Upvote 0

Forum statistics

Threads
1,215,949
Messages
6,127,877
Members
449,410
Latest member
adunn_23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top