MsScript - jscript code, run-time error 1014 - invalid character
Trying to adapt msscript control and run jscript code functions, but stuck with 'infamous' 1014 invalid character error. Can't see where the problem lies in the code, as code below runs properly in browser.
Any idea what might be wrong with the following javascript syntax?
Code:
function FIVATCheckDigit (vatnumber) {
var total = 0;
var multipliers = [7,9,10,5,8,4,2];
for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];
total = 11 - total % 11;
if (total > 9) {total = 0;};
if (total == vatnumber.slice (7,8))
return true
else
return false;
}
Nothing wrong with the JScript-code itself - but you have a character
with the Ascii-Code &H01 in it (SOH) - shortly before your closing "curly brace" of the func-definition.
With a somewhat better Error-Handling (the ScriptControl supports an Error-Object which informs you
about the Line- and ColumnNumber), you'd have found that yourself...
E.g. when you change the complete Form-code in your Zip to:
Code:
Option Explicit
Private Sub Command1_Click()
Dim ScriptControl1 As ScriptControl
Set ScriptControl1 = New ScriptControl
With ScriptControl1
.Language = "Javascript" 'Set script language.
.AllowUI = False 'Reset UI interaction (TRUE is the default).
'.AddObject "Editor", Form1.Text1
On Error Resume Next
.AddCode Text1.Text 'Copy the script code to the control.
If Err Then
HandleError .Error
Else
'.Run "FIVATCheckDigit", "09508951"
Caption = .Run("FIVATCheckDigit", "09508951")
End If
End With
End Sub
Private Sub HandleError(Error As Object)
Dim Line, LineNr As Long, SelPos As Long
For Each Line In Split(Text1.Text, vbCrLf)
LineNr = LineNr + 1
If Error.Line = LineNr Then Exit For
SelPos = SelPos + Len(Line) + 2
Next
Text1.SelStart = SelPos + Error.Column
Text1.SelLength = 1
Text1.SetFocus
MsgBox "ScriptErr(" & Error.Line & ":" & Error.Column + 1 & ") " & Error.Description
End Sub
You'd be given a hint where exactly the error happened...
... but you have a character
with the Ascii-Code &H01 in it (SOH) - shortly before your closing "curly brace" of the func-definition.
I suspect this is how it got there:
When the JScript code was pasted in the VB.TextBox's Text property editor, VB6 silently replaced the closing curly brace on the last line with a SOH character. Apparently, VB6 has a bug (or undocumented feature?) that causes it to turn curly braces (both opening and closing) to SOH characters when the following conditions are true:
The TextBox control's MultiLine property is True.
The curly brace is the only character on a line.
The text was assigned to the control via the IDE's Properties Window.
Tech99 noticed the last curly brace is missing so he added it back. He probably didn't noticed the SOH character though, which looked like a space character.
The fix for that VB6 bug is to ensure a curly brace is never alone on a line (adding a space or any other suitable character before or after prevents the bug).
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Yes, excellent finding. I was able to reproduce that behaviour constantly. It didn't occured to me, that there could be this kind of behaviour, when pasting ordinary ansi multiline text. It is revealing to look form frx file using fex. notepad++ or hex editor.
Schmidt and Bonnie thank you.
Have to look that David Zimmer's Duktape debugger.