Erreur d'exécution VBA 3704 avec jeu d'loggings dans Excel

Bonne journée.

Lorsque ce code frappe "While Not objMyRecordset.EOF", je reçois l'erreur de time d'exécution 3704. En plus de cela, lorsque je passe le slider sur la partie "objMyRecordset" de strPSTPath = CStr (objMyRecordset ("PSTPath")) Je vois l'erreur en commençant "objMyRecordSet (PS … =

Ma requête SQL fonctionne bien lorsqu'elle est utilisée dans le studio de gestion de servers SQL. L'erreur se produit instantanément lorsque vous appuyez sur la ligne en question. J'ai parcouru le code ligne par ligne. Toute reflection sera apprécié. Je vous remercie.

Sub Button3_Click() '****** 'Variables Set objMyConn = New ADODB.Connection Set objMyCmd = New ADODB.Command Set objMyRecordset = New ADODB.Recordset 'Open connection objMyConn.ConnectionSsortingng = "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 

Essaye ça:

 Sub Button3_Click() Dim SQL As Ssortingng, strPSTPath As Ssortingng 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 

Cela fait longtime que j'utilise VBA et ADODB, mais au lieu d'ouvrir l'object du jeu d'loggings, je crois que vous devez exécuter l'object de command:

 set objMyRecordset = objMyCmd.Execute 

Jetez un oeil à ce lien .

Suivi le problème. La string de connection était incorrecte pour la version de SQL sur ce server particulier. Changé la string de connection à:

Driver = {SQL Server Native Client 11.0}; Server = XXXX; Database = XXXX; Trusted_Connection = yes;

Maintenant, tout fonctionne.