Hi there ,
I would like to get some data from an SQL server and found this code from MS.
I just adjusted it for my needs , but it fails when opening the table Historic - Full.
The tablename has a space before and after the - (minus sign).
This is the tablename : "Historic - Full"
Can someone help me how to fix this pls.
Renaming the table on SQLserver is not an option.
thanks in advance
Mario
Sub DataExtract()
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=S0;INITIAL CATALOG=sprinters_live;"
'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"
'Now open the connection.
cnPubs.Open strConn
' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "SELECT * FROM Historic - Full"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A1").CopyFromRecordset rsPubs
' Tidy up
.Close
End With
End Sub
Bookmarks