I have one query of SQL
, which I want to put in a VBA
code to bring result in Excel sheet.
I tried the code pasted below.
Sub Connect2SQLXpress()
Dim Ors As Object Dim oCon As Object
Set oCon = CreateObject("ADODB.Connection")
Set Ors = CreateObject("ADODB.Recordset")
Set oCon = New ADODB.Connection oCon.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI"
oCon.Open
Ors.Source = "Select KeyInstn,LongName,Shortname,Shortestname From New_inst WHERE ......"
Ors.Open Range("A1").CopyFromRecordset Ors
Ors.Close oCon.Close
If Not Ors Is Nothing Then Set Ors = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing
Can someone please help me in figuring out that what is going wrong ??