DIAdem Script: In For Loop, Function Call Using Channel Index Does Not Give Results

Updated Dec 5, 2025

Reported In

Software

  • DIAdem Full
  • DIAdem Base
  • DIAdem Professional

Programming Language

  • Visual Basic

Issue Details

I have a problem with a script in DIAdem when I try to call a function within a for loop. My function call is similar to this:

 
For groupIndex = 1 to 10
Call chnCalculate("ch(""[groupIndex]/channelName"")= ..... ")
Next


I do not get any results if I use the loop counter variable groupIndex as an index for the group, but my code works as expected when I index the group with a hard-coded number. Is this a limitation with the code? 

Solution

The root cause of this issue is the string formatting. chCalculate function takes a formula as input in the form of a string data type. We have to ensure the formula string is formatted correctly to obtain the expected outcome. Here are two different approaches to insert a group index variable into your formula string :

  • Approach 1: Concatenate String with Variable
Dim groupIndex, channelName
channelName = "Speed"
For groupIndex = 1 to 10
  Call ChnCalculate("Ch(""[" & groupIndex & "]/Result"")= 2 * Ch(""[" & groupIndex & "]/"&channelName&""")")
Next
  • Approach 2: Pre-activate Channel Group
Dim groupIndex, channelName
channelName = "Speed"
For groupIndex = 1 to 10
  Data.Root.ChannelGroups(groupIndex).Activate
  Call ChnCalculate("Ch(""/Result"")= 2 * Ch(""/"&channelName&""")")
Next
     
     

    Additional Information

    • If you reference the group by name often, these approaches could make your code cluttered. Consider creating an object for the active channel with the following statement: 
    Set oChnGrp = Data.Root.ChannelGroups(groupIndex)
     
    • The two solutions to this problem give the same results - pick the one you prefer based on your style of coding.