Sub MultiResultProcTest()
Dim Conn As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim Cmd As New ADODB.Command
Dim Row As Long
Dim Findex As Long
Dim Data As Worksheet
Dim X As Long
Dim Server As String
On Error Resume Next
Application.Calculation = xlCalculationManual
Set Data = Sheets(ActiveSheet.Name)
Data.Select
Cells.ClearContents
Server = "SYDSBMESQL002"
DatabaseUserName = "sa"
DatabasePassword = "admin"
Conn.Open "PROVIDER=SQLOLEDB;DATA SOURCE=" & Server & ";USER ID=" & DatabaseUserName & ";PASSWORD=" & DatabasePassword
Cmd.ActiveConnection = Conn
Cmd.CommandType = adCmdText
Cmd.CommandText = "BanditStaging.dbo.SP_PRODUCT_CHECK_UPC '%9332727018176%'"
Set RS = Cmd.Execute
For Y = 1 To 4
X = 0
For X = 0 To RS.Fields.Count - 1
Data.Cells(1 + (Y * 4), X + IIf(ActiveSheet.Name = "Main", 1, 2)) = RS.Fields(X).Name
Next
'If the result set is smaller than the sheet then populate in one go otherwise post line by line until the sheet is full
If RS.RecordCount < Rows.Count Then
Data.Cells(Row + 2 + (Y * 4), Findex + IIf(ActiveSheet.Name = "Main", 1, 2)).CopyFromRecordset RS
Else
Do While Not RS.EOF
Row = Row + 1
For Findex = 0 To RS.Fields.Count - 1
Data.Cells(Row + 1 + (Y * 4), Findex + IIf(ActiveSheet.Name = "Main", 1, 2)) = RS.Fields(Findex).Value
Next Findex
RS.MoveNext
Loop
Row = 0
End If
Set RS = RS.NextRecordset() '<-- I forgot the word "Set" at the start of here
Next
Application.Calculate
Application.Calculation = xlCalculationAutomatic
End Sub