High Plains Grifter
Board Regular
- Joined
- Mar 9, 2010
- Messages
- 129
Hi all,
I have a workbook which contains a link to an external SQL procedure to bring in data. The user will select parameters in the spreadsheet and run the macro to retrieve the data and then will be shown a pivot table of the results. The data actually goes initially into a normal table, which is in a hidden sheet. This hidden table contains some calculated fields and things which are used to make the pivot table the user can see and edit. I have done this so that there is a nice selection of neatly named fields etc for the user to play with when editing the pivot table.
Here is the code which retrieves the data and shows the pivot table (which is in the sheet "Results")
The problem is with the last 3 lines. for some reason, the refresh command seems to only show be the previous data rather than the current data in the pivot table when the macro is run in the normal way (there is a button in the workbook). For instance, here is a chronology of something that might happen:
If I were to run the macro with ID "A", I would see arbitrary previous data.
If I then ran the macro again IDs "B,C,D", I would see "A"s data.
If I then ran with IDs "E,F" I would see "B,C,D"s data.
If I ran the macro again, without changing the people to be displayed, I would see "E,F"s data in the pivot table
In other words, the data is always one "run" behind what it should be. Weirdly, if I run the macro either by putting a break point at the line "pt.RefreshTable" or by stepping through the code, the pivot table updates correctly and everything is fine.
I have checked the table that the pivot table refers to and that definitely updates with the connection refresh. I confess myself flummoxed. Any ideas what is going on?
Thanks for your time!
Mark
I have a workbook which contains a link to an external SQL procedure to bring in data. The user will select parameters in the spreadsheet and run the macro to retrieve the data and then will be shown a pivot table of the results. The data actually goes initially into a normal table, which is in a hidden sheet. This hidden table contains some calculated fields and things which are used to make the pivot table the user can see and edit. I have done this so that there is a nice selection of neatly named fields etc for the user to play with when editing the pivot table.
Here is the code which retrieves the data and shows the pivot table (which is in the sheet "Results")
Code:
Sub RefreshQuery()
Dim strSQL As String
Dim pt As PivotTable
'If they don't tell us what they want, hit them around the head with a message bok or two
On Error Resume Next
'If the first user is blank, then the chosen user range is empty and they have selected no one
If Range("FirstUser").Value = "" Then
MsgBox "You must select at least one user to report against", vbCritical, "Error Retrieving Data"
Exit Sub
Else
If Range("StartDate").Value = "" Or Range("FinishDate").Value = "" Then
MsgBox "You must enter a complete date range to report against", vbCritical, "Error Retreiving Data"
Exit Sub
Else
'nothing
End If
End If
'From this point on, errors will be my fault, so give them some helpful advice if the data fails
On Error GoTo Fail
'build the command string to suit the procedure (EXEC [User_GET_TestProductivityStats] @UserIds,@Start,@Finish,@Grouper)
strSQL = _
"EXEC gotrex_companion_live.dbo.[User_GET_TestProductivityStats] '" & _
Range("ConcatUserList").Value & "','" & _
Format(Range("StartDate").Value, "yyyy-MM-dd") & "','" & _
Format(Range("FinishDate").Value, "yyyy-MM-dd") & "','" & _
Application.WorksheetFunction.Index(Range("Grouping"), Range("GroupingSelected").Value) & "'"
'If you want to see the command string
'Sheets("Parameters").Range("I1").Value = strSQL
'Exit Sub
With ActiveWorkbook.Connections("UsersActionsExample")
.OLEDBConnection.CommandText = Array(strSQL)
.Refresh
End With
'Tell them what to do if it all goes wrong.
Fail:
If Err.Number <> 0 Then
MsgBox "Data Retrieval Failed " & vbCrLf & _
"Please check date formats and grouping. If you cannot see a problem with the data you have entered, please contact GoTrex Support on 1468" & vbCrLf & _
"Error Code: " & Err.Number, vbExclamation, "SQL Procedure Error"
Exit Sub
Else
'No problem - carry on
End If
'Show them the numbers
Sheets("Results").Activate
Set pt = ActiveSheet.PivotTables("PivotTable3")
'Refresh the pivot table.
pt.RefreshTable
End Sub
The problem is with the last 3 lines. for some reason, the refresh command seems to only show be the previous data rather than the current data in the pivot table when the macro is run in the normal way (there is a button in the workbook). For instance, here is a chronology of something that might happen:
If I were to run the macro with ID "A", I would see arbitrary previous data.
If I then ran the macro again IDs "B,C,D", I would see "A"s data.
If I then ran with IDs "E,F" I would see "B,C,D"s data.
If I ran the macro again, without changing the people to be displayed, I would see "E,F"s data in the pivot table
In other words, the data is always one "run" behind what it should be. Weirdly, if I run the macro either by putting a break point at the line "pt.RefreshTable" or by stepping through the code, the pivot table updates correctly and everything is fine.
I have checked the table that the pivot table refers to and that definitely updates with the connection refresh. I confess myself flummoxed. Any ideas what is going on?
Thanks for your time!
Mark