Is there a way to purge a data provider through VBA? I want to have the code check the date when the document is open and based on a condition (such as beginning of the week), purge the data from the report and have the report refresh using the data for the new week.
I could possibly be missing something in your requirement, but there would be no need to purge data first. Refreshing the report completely replaces any previously existing data.
The requirement from our user is to have a report to view inventory quantities per month, but to only refresh the current month while keeping the data for previous months in the report. If we were to refresh for all months each time, by the end of the year the report would take an inordinate amount of time to run. So I have created a dataprovider for each month and a script to run only a given DP at a time. Leaving the other month’s data static on the report. What I want to do is at the beginning of a new year, purge all data providers and begin with a fresh report.
That helps a lot actually. I can think of three solutions:
[list]1. Just start with an empty report from the corporate repository … no muss, no fuss.
2. I assume you are setting the IsRefreshable property to control which are refreshed, right? What would happen if all are refreshed at the beginning of the year? Wouldn’t Feb - Dec run quickly and retrieve no data?
3. I can’t find a purge method either. Here is a total kludge, but given the proper user privileges, this code will effectively purge data providers. It simulates doing a “save and close” on each data provider and choosing yes to the purge question.[/list]
Sub PurgeDataProviders()
Dim DataProv as busobj.DataProvider
For Each DataProv in ThisDocument.DataProviders
If DataProv.GetType = "DPQTC" Then
SendKeys "%s%y"
DataProv.Edit
Next DataProv
End Sub
Sub PurgeDataProviders()
Application.Interactive = False
Dim DataProv As busobj.DataProvider
Dim MaxLines As Long
For Each DataProv In ActiveDocument.DataProviders
If DataProv.GetType = "DPQTC" Then
MaxLines = DataProv.MaxNbLines
DataProv.MaxNbLines = 0
DataProv.MaxDuration = 0
DataProv.Refresh
DataProv.MaxNbLines = MaxLines
DataProv.MaxDuration = MaxLines
End If
Next DataProv
Application.Interactive = True
End Sub
Looks like a neat solution. I don’t have time to test it, but I wonder if it actually runs the query anyway, and just doesn’t return any records. That would be effective, but a lot of unnecessary queries.
The “Do you want to purge ?” box appeared right, and it did choose “yes”, but looking in the cube the data was still here…
So i used your sendkeys idea to do it by the cube directly :
' The letters called in the SendKeys are for the french version
' Find the right letters for your version
Sub Purge()
SendKeys "%d" 'Data menu
SendKeys "l" 'Open the cube
SendKeys "{TAB}{TAB}{TAB}{TAB}{TAB}" 'Navigate to the dataproviders list
For i = 0 To ActiveDocument.DataProviders.Count
SendKeys "%p%o" 'Purge + Yes
SendKeys "{DOWN}" 'Next Dataprovider :o)
Next
End Sub
Note the 5x{tab} won’t work if the first dataprovider is already empty - the Purge button will be greyed out so doesn’t get tabbed to (in 6.5.1, anyway). You can use 7x+{tab} (i.e. Shift+Tab) instead though, if you’re expecting any empty dataproviders.