Sub ConnectToSQLServer()
Dim adoCN As ADODB.Connection, adoRS As ADODB.Recordset
Dim strSQL As String
Set adoCN = New ADODB.Connection
'///////////////////////////////////////////////////////////
'CRUCIAL PART - ONCE YOU GET THIS WORKING YOU'RE LAUGHING!
'This part will open a connection to a SQL Server database
'You will need to change the Data Source, Initial Catalog,
'user ID and password to suit your needs
adoCN.Open "Provider=sqloledb;" & _
"Data Source=ertyerty;" & _
"Initial Catalog=res_db;" & _
"User Id=ASDFGG;" & _
"Password= "ertyertyerty ''
'///////////////////////////////////////////////////////////
'Now that the connection is open, you can create recordsets and work
'with them in Excel e.g.
Set adoRS = New ADODB.Recordset
'Change this SQL statement to whatever you need
strSQL = "SELECT Security, * FROM res_db.dbo.Instruments where security = 'MSFT.US'"
'##################################################
'THIS IS WHERE I'VE INSERTED THE NEW CODE 20-JAN-11
Const sSQL As String = "SELECT Security, Price FROM res_db.dbo.Instruments where security = 'zz'"
Dim sCode As String
sCode = Application.InputBox(Prompt:="Stock Code?", Type:=2)
strSQL = Replace$(sSQL, "zz", sCode)
'##################################################
'Open the recordset
adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
'Copy the recordset into a worksheet (will only work with Excel 2000 onwards)
ActiveCell.CopyFromRecordset adoRS
'You should disconnect from the database at the end of any code which opens
'it.Leaving a connection open could cause you problems.
adoRS.Close
adoCN.Close
End Sub