[英]VBA run time error 3704 with recordset in Excel
Good day. 美好的一天。
When this piece of code hits "While Not objMyRecordset.EOF", I receive run time error 3704. In addition to this, when I hover over the "objMyRecordset" portion of "strPSTPath = CStr(objMyRecordset("PSTPath"))", i see error beginning "objMyRecordSet(PS... = 当这段代码命中“ While Not objMyRecordset.EOF”时,我收到运行时错误3704。此外,当我将鼠标悬停在“ strPSTPath = CStr(objMyRecordset(“ PSTPath”))”的“ objMyRecordset”部分时,我看到以“ objMyRecordSet(PS ... =
My SQL query works fine when used in SQL server management studio. 在SQL Server Management Studio中使用时,我的SQL查询工作正常。 The error occurs instantaneously upon hitting the line in question.
遇到问题时,该错误立即发生。 I have stepped through the code line by line.
我一步一步地完成了代码。 Any thoughts would be appreciated.
任何想法将不胜感激。 Thank you.
谢谢。
Sub Button3_Click()
'******
'Variables
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
'Open connection
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=XXXX;Data Source=XXXX"
objMyConn.Open
'Set and execute SQL command
Set objMyCmd.ActiveConnection = objMyConn
objMyCmd.CommandText = "<Valid SQL Command removed for public code display>"
objMyCmd.CommandType = adCmdText
'Open recordset
Set objMyRecordset.Source = objMyCmd
objMyRecordset.Open objMyCmd
While Not objMyRecordset.EOF
strPSTPath = CStr(objMyRecordset("PSTPath"))
MsgBox strPSTPath
objMyRecordset.MoveNext
Wend
End Sub
Try this: 尝试这个:
Sub Button3_Click()
Dim SQL As String, strPSTPath As String
Dim objMyConn, objMyRecordset
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
objMyConn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;" & _
"Persist Security Info=False;Initial Catalog=XXXX;Data Source=XXXX"
SQL = "<Valid SQL Command removed for public code display>"
objMyRecordset.Open SQL, objMyConn
While Not objMyRecordset.EOF
strPSTPath = CStr(objMyRecordset("PSTPath"))
MsgBox strPSTPath
objMyRecordset.MoveNext
Wend
End Sub
Tracked down the problem. 找出问题所在。 The connection string was incorrect for the version of SQL on this particular server.
对于此特定服务器上的SQL版本,连接字符串不正确。 Changed the connection string to:
将连接字符串更改为:
Driver={SQL Server Native Client 11.0};Server=XXXX;Database=XXXX;Trusted_Connection=yes; 驱动程序= {SQL Server Native Client 11.0};服务器= XXXX;数据库= XXXX; Trusted_Connection =是;
Now everything works. 现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.