jebenexcel
Board Regular
- Joined
- Mar 16, 2018
- Messages
- 59
Hello,
I need to import data from an SQL database into an excel spreadsheet.
The query works fine in Microsoft SQL Management Studio, but I don't know how to get VBA to paste it into cell A1 of Sheet1.
I have already created the connection using data-> From other Sources->from SQL server.
This code used to do it, but I have to change it to a different server with different column names and now it doesn't work.
I need to import data from an SQL database into an excel spreadsheet.
The query works fine in Microsoft SQL Management Studio, but I don't know how to get VBA to paste it into cell A1 of Sheet1.
I have already created the connection using data-> From other Sources->from SQL server.
This code used to do it, but I have to change it to a different server with different column names and now it doesn't work.
Code:
Sub RefreshSQL()
Dim newsqltext As String
newsqltext = "(query)"
With ActiveWorkbook.Connections("SQL connection").OLEDBConnection
.CommandText = newsqltext
End With
ActiveWorkbook.RefreshAll ' Refresh
End Sub