Skip to main content

Summarization function

Hi all,
I have created a function for summarization task (Function Summarize) that is called from other Function (PerformAction) - the main aim is to add loop that will be setting 3 different fields for sumarization tasks - task.AddFieldToSummarizet and task.AddFieldToTotal  (task is a variable Action2A_Summarize).Does anyone came up with a code that passes array of options within the loop for those 2 task within summarization task?
My code so far  looks like this but I think it will fail:
          Function PerformAction(MasterDb As Object,TaskName As String,Criteria As String)
Dim TargetDb As Object
                Dim Arr_FieldtoInclude(3)  as String
               Dim FieldsLength as Integer
Set TargetDb = MasterDb
                'calling some other functions here
                FieldsLength = 3
               Redim 
               Arr_FieldtoInclude(FieldsLength)
               Arr_FieldtoInclude(0) = "Amount"
               Arr_FieldtoInclude(1) = "Debtors"
              Arr_FieldtoInclude(1) = "Creditors"
                Call Summarize(Arr_FieldtoInclude as String ,FieldsLength as Integer)
               End Function
             
 
               'Function for Summarizing task
                Function Summarize(TargetDb As Object,Arr_FieldtoInclude as String ,FieldsLength as                            Integer)
               Dim Action2A_Summarize As Task ' Set the name of the task
                 Set Action2A_Summarize = TargetDb.Summarization 'Set the summarization task
                For i = 1 to (FieldsLength+1)
                Action2A_Summarize.AddFieldToSummarize Arr_FieldtoInclude(i)
                Next i
For i = 1 to (FieldsLength+1)
                 Action2A_Summarize.AddFieldToTotal Arr_FieldtoInclude(i)
                 Next i
dbName = "Summarization.IMD"
Action2A_Summarize.OutputDBName = dbName
Action2A_Summarize.CreatePercentField = FALSE
Action2A_Summarize.StatisticsToInclude = SM_SUM + SM_MAX + SM_MIN
Action2A_Summarize.PerformTask
Set task = Nothing
Set db = Nothing
Client.OpenDatabase (dbName)
 
           End Function
 

Brian Element Thu, 06/14/2018 - 19:53

Hi Pawelski_33,

Right now it will fail for a few reasons, you aren't including the database object in your Summarize call, you are also not sending the Arr_FieldToInclude.  You also don't need to include the number of items in the array, for the for loop you could do something like this, also the default is the first item is a 0 so if you use a 1 you will miss the first item.

For i = 1 to ubound(Arr_FieldToInclude)

     Action2A_Summarize.AddFieldToTotal Arr_FieldToInclude(i)

next i

When you defined your array you defined it as 3 which means there are 4 items (including 0), since you only have 3 the definitation should have been 2.

Can you give me more info on what you mean by array of options?  I think I might understand what you want but I just want to make sure.

Brian

Pawelski_33 Fri, 06/15/2018 - 04:31

Hi Brian,
Thanks for your message - I only read it now so couldnt take advantage of your points as I had to fix the code yesterday but I got there in the end i think.
Array definition and defining the elements of the array was not a big issue really - my biggest problem was passing the arguments from the function Summarize to the section of function PerformSummarize_and_Export, from which I call the Summarize fuction.I found out now that when the function is called you need only provide the names of the arguments and not the data type. The data type for function arguments is only required for the actual function definition (it would be my suggestion for you to create a video about passing the arguments within functions (if you agree its a interesting topic) as it is a bit of issue in IDEA for a new users like me).
Incorrect way passing of arguments (I added data type in section where i called function) caused my array elements Arr_FieldsToInclude(i-1) not to be picked up  in the section of code that specifies which fields to add to summarize and fields to add to Total in Summarize function.
Please have a look at the attached and let me know if you have any comments to improve it.
Regards
Pawel

The website encountered an unexpected error. Try again later.