我是Excel VBA的新手。我已经做好了代码,是为IP地址为199.63.106.70的机器编写的。现在,我希望在另一台机器199.56.122.155上运行相同的代码。我已经在新机器上安装了MS SQL server2008 R2。我还使用数据连接向导检查了connection。获取数据。但当我尝试通过点击按钮获取数据时,它显示错误消息"Error in Process“。控制器正在从oCon.Open跳转,如何解决此错误?连接字符串的格式是否正确?用户Id和密码是windows登录凭据,以字符串形式使用。
Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim rowcounter As Long
On Error GoTo errhandler
rowcounter = 2
Set oCon = New ADODB.Connection
oCon.connectionstring = "Driver={SQL Server};Server=199.63.106.70;Database=dashboard;User Id=dashboardadmin;Password=passwrd;"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon
oRS.Source = "SELECT HourlyReadingTimestamp, Hourlyreading,cost FROM MeterConsumptioNDetail where meterid=" & Range("J5").Value & " and HourlyreadingTimestamp between '" & Range("K5").Value & "' and '" & Range("L5").Value & " 23:59:59' order by HourlyreadingTimestamp"
oRS.Open
While Not oRS.EOF
Range("A" & rowcounter).Value = oRS.Fields(0).Value
Range("B" & rowcounter).Value = oRS.Fields(1).Value
Range("C" & rowcounter).Value = oRS.Fields(2).Value
rowcounter = rowcounter + 1
oRS.MoveNext
Wend
oCon.Close
If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing
MsgBox ("Data fetched successfully")
Exit Sub
errhandler:
MsgBox ("Error in process!")
End Sub发布于 2015-01-21 00:21:50
如果要使用集成安全性(Windows登录),则连接字符串不正确,请使用:
Driver={SQL Server};Server=199.63.106.70;Database=dashboard;Trusted_Connection=Yes; 驱动程序将处理基于运行进程的用户的身份验证。
https://stackoverflow.com/questions/28046882
复制相似问题