C1FlexReport - Rudimentary Function throwing errors

Posted by: mcherry on 2 April 2020, 3:21 am EST

  • Posted 2 April 2020, 3:21 am EST

    I suspect I don't use VBScript enough to identify why this isn't working. I'm getting numerous errors in trying to write a custom function in Report.GlobalScripts

    It's not a particularly complex function, but here's
    Function FormatTime(ByRef totalSeconds)
    Dim hours = totalSeconds / 3600
    Dim minutes = (totalSeconds mod 3600) / 60
    Dim seconds = totalSeconds mod 60

    FormatTime = hours + ":" + minutes + ":" + seconds
    End Function

    This gives me an error that variables are being used before they're initialized. Breaking assignment away from declaration seems to work. I say seems to, because the error changes.

    Now I get "object reference not set to an instance of an object" - but there's no further information and I can't debug to see what's not set, where. So, if I don't call the function it doesn't happen, and if I do I get that error even dumbing it down to just this:
    Function FormatTime(totalSeconds)
    FormatTime = totalSeconds
    End Function


    Is this not VBScript? Or VBA? I'm at a total loss for what could possibly be wrong here, at this point. I work in C# normally and every reference I look at for VBScript says this syntax is the correct way to return a value from a function.
  • Replied 2 April 2020, 4:34 am EST

    Somehow changing from ByRef to ByVal makes all the difference and now it works just fine. Not clear on why, since null entries are set to 0 by the data source, is 0 treated as null?
  • Replied 2 April 2020, 5:49 am EST

    well, this function works in one report and just doesn't in another
    Function FormatTime(ByVal totalSeconds)

    dim hrs as vbString
    dim mns as vbString
    dim sec as vbString

    hrs = Format(int(totalSeconds / 3600), "##00")
    mns = Format(int((totalSeconds / 60) mod 60), "##00")
    sec = Format(int((totalSeconds) mod 60), "##00")

    FormatTime = hrs + ":" + mns + ":" + sec
    End Function
  • Replied 2 April 2020, 5:52 am EST

    oh the last one seems to be because I'm using the same function in a subreport and it's trying to call that function instead of the one defined in this report...hm. So the real question is: is there an actual Global script that's shared between Reports?
  • Marked as Answer

    Replied 5 April 2020, 6:35 pm EST

    Hi Mark,

    Sorry but the global script is not shared among reports.

    >>Not clear on why, since null entries are set to 0 by the data source, is 0 treated as null?
    The ByVal sends a copy of the argument’s value to the procedure.
    The ByRef sends a reference indicating where the value is stored in memory which is not the case in the script.
    http://ecomputernotes.com/visual-basic/oops/explain-the-difference-between-byval-and-byref-when-is-each-used

    Regards,
    Prabhat Sharma.
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels