Getting Error at cmd.Execute

  • Thread starter Thread starter Saritask
  • Start date Start date
S

Saritask

Guest
Hi All,

I am new to VBA a trying to execute .sql query from Excel. Below is the Query Written using VB but I am getting Runtime error at cmd.Execute statement.


Sub GetQueryResults()


Dim Fso As Scripting.FileSystemObject
Dim Ts As Scripting.TextStream
Dim QueryString As String

Set Fso = New Scripting.FileSystemObject
Set Ts = Fso.OpenTextFile("C:\Users\sarita.h.kumari\Documents\P&WC\Payment Terms Review\PAYMENT tERms.sql")

QueryString = Ts.ReadAll

Ts.Close
Dim Cn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim cmd As Object

Set Cn = New ADODB.Connection
Set cmd = CreateObject("ADODB.Command")
cmd.CommandType = 1 ' adCmdText
cmd.CommandTimeout = 120 ' number of seconds to time out

Cn.ConnectionString = _
"Provider=MSOLEDBSQL;" & _
"Server=M2C-L-524457Y;" & _
"Database=Master;" & _
"Trusted_Connection=Yes;"

Cn.Open

cmd.CommandText = QueryString
cmd.ActiveConnection = Cn
cmd.Execute


Cn.Close

End Sub

Continue reading...
 
Back
Top