2
votes

I've lifted the following code from a thread here DateDiff in days, hours and mins

but I'm getting a syntax error on the first line of code and I can't work out why because it looks the same as other function syntax I've written that works fine.

Function TimeSpan(dt1, dt2) 

        seconds = Abs(DateDiff("S",dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes mod 60 
        seconds = seconds mod 60 

        if len(hours) = 1 then hours = "0" & hours 

        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 

End Function 

dt1 = ActiveRequest.Fields("CreationDate").Value
dt2 = ActiveRequest.Fields("1stcontactdatetime").Value

The error I receive is: "Error in control script at line 1: Syntax error (Microsoft VBScript compilation error): FuntionTimeSpan(dt1, dt2)"

Any advice would be greatly appreciated, especially if it helps me not do it again!

1
What scripting host are you using? - Bond
@Bond I have run the following code MsgBox(ScriptEngine) MsgBox(ScriptEngineBuildVersion) MsgBox(ScriptEngineMajorVersion) MsgBox(ScriptEngineMinorVersion) code I received the following VBScript, 17280, 5, 8 I believe it works with Windows Scripting Host - Peter
OK, so you're using WSH? Is it a VBS file or a WSF? Is your code contained in a single file or are you using script components? The "Error in control script" is not something I've seen often. - Bond
I am writing the script inside of another program and it's objects can be address and manipulated by VBScript. As such I don't really get to see my script as you've described but I'm inclined to think it's WSH based on the behaviour. - Peter
It's just odd because it sounds like it's erroring on the function declaration itself. Are you sure the program you're writing your script within doesn't already define a function named TimeSpan? - Bond

1 Answers

1
votes

Make it a sub or define a return variable.

Though my vbscript engine doesn't mind your code.

You are probably using the msscript.ocx (MS Script Control), it runs vbscripts in an application. Probably a help file in system32 for it.

Timespan is a class in the .NET framework.

The final thing is if using an edit control box, as notepad is and dialogs do, you get character problems. Paste into wordpad (based on a richtext edit control) so you can check for formatting edit controls ignore.

In the error message VBScript thinks there is no space between function and the function name.