I can't import values values from SQL Server to Excel. Can someone please help:
Dim oConn As ADODB.Connection
Private Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "Provider=SQLOLEDB; " & _
"Data Source=ServerName; " & _
"Initial Catalog=MyDB;" & _
"Trusted_Connection=yes;"
End Sub
Public Sub ExportDataToDB()
Dim rs As ADODB.Recordset
ConnectDB
Set rs = New ADODB.Recordset
Dim strSql As String
strSql = "select t.col1, t.col2 from Table t"
rs.Open strSql, oConn
Sheet1.Range("A2").CopyFromRecordset rs
CloseDBConn
End Sub
Private Sub CloseDBConn()
oConn.Close
End Sub
Connection is successful. No errors. Query is not empty, but nothing is returned into sheet.
Table??rs.RecordCountreturn? Also might be worth checking ifSheet1belongs to where you think belongs.Sheet1?