Je rencontre des difficultés pour récupérer toutes les colonnes lorsque j'exécute le code suivant dans excel vba. Je ne récupère que 6 colonnes sur 23. La connexion, la commande etc fonctionne bien (je peux voir la commande exec dans le profileur SQL), les en-têtes de données sont créés pour toutes les 23 colonnes mais je ne reçois que des données pour 6 colonnes.Exécution de SQL SP dans Excel VBA
Side Note: il est code de niveau prod non, ont manqué la gestion des erreurs sur le but, sp fonctionne très bien en studio de gestion SQL, ASP.Net, C# win forme app, il est pour Excel 2003 se connecter à SQL 2008.
Quelqu'un peut-il m'aider à résoudre le problème?
Dim connection As ADODB.connection
Dim recordset As ADODB.recordset
Dim command As ADODB.command
Dim strProcName As String 'Stored Procedure name
Dim strConn As String ' connection string.
Dim selectedVal As String
'Set ADODB requirements
Set connection = New ADODB.connection
Set recordset = New ADODB.recordset
Set command = New ADODB.command
If Workbooks("Book2.xls").MultiUserEditing = True Then
MsgBox "You do not have Exclusive access to the workbook at this time." & _
vbNewLine & "Please have all other users close the workbook and then try again.", vbOKOnly + vbExclamation
Exit Sub
Else
On Error Resume Next
ActiveWorkbook.ExclusiveAccess
'On Error GoTo No_Bugs
End If
'set the active sheet
Set oSht = Workbooks("Book2.xls").Sheets(1)
'get the connection string, if empty just exit
strConn = ConnectionString()
If strConn = "" Then
Exit Sub
End If
' selected value, if <NOTHING> just exit
selectedVal = selectedValue()
If selectedVal = "<NOTHING>" Then
Exit Sub
End If
If Not oSht Is Nothing Then
'Open database connection
connection.ConnectionString = strConn
connection.Open
' set command stuff.
command.ActiveConnection = connection
command.CommandText = "GetAlbumByName"
command.CommandType = adCmdStoredProc
command.Parameters.Refresh
command.Parameters(1).Value = selectedVal
'Execute stored procedure and return to a recordset
Set recordset = command.Execute()
If recordset.BOF = False And recordset.EOF = False Then
Sheets("Sheet2").[A1].CopyFromRecordset recordset
' Create headers and copy data
With Sheets("Sheet2")
For Column = 0 To recordset.Fields.Count - 1
.Cells(1, Column + 1).Value = recordset.Fields(Column).Name
Next
.Range(.Cells(1, 1), .Cells(1, recordset.Fields.Count)).Font.Bold = True
.Cells(2, 1).CopyFromRecordset recordset
End With
Else
MsgBox "b4 BOF or after EOF.", vbOKOnly + vbExclamation
End If
'Close database connection and clean up
If CBool(recordset.State And adStateOpen) = True Then recordset.Close
Set recordset = Nothing
If CBool(connection.State And adStateOpen) = True Then connection.Close
Set connection = Nothing
Else
MsgBox "oSheet2 is Nothing.", vbOKOnly + vbExclamation
End If
Quelle valeur renvoie recordset.Fields.Count? –