Results 1 to 6 of 6

Thread: MsScript - jscript code, run-time error 1014 - invalid character

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    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;
    }
    Attached is the test project.

    If someone could steer how to debug jscript code?
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: MsScript - jscript code, run-time error 1014 - invalid character

    jScript question in a VB forum????

    I have used JavaScript but never even looked at JScript.

    Looking at your script though it seems you are missing a ; and a set of {}

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: MsScript - jscript code, run-time error 1014 - invalid character

    Yeah, jscript question in a VB forum - true.

    Solved this already. Somehow errors 'deals with' the jscript code input as textbox text.

    When jscript code is given in string variable, code parses and runs ok.

    Code:
    Private Sub Command1_Click()
    'Debug.Print FIVATCheckDigit("09508951")
    Dim ScriptControl1 As ScriptControl
    Set ScriptControl1 = New ScriptControl
    Dim jsCode As String
    
    jsCode = "function FIVATCheckDigit (vatnumber) {" & vbCrLf
    jsCode = jsCode & "  var total = 0;" & vbCrLf
    jsCode = jsCode & "  var multipliers = [7,9,10,5,8,4,2];" & vbCrLf
    jsCode = jsCode & "  for (var i = 0; i < 7; i++) total += Number(vatnumber.charAt(i)) * multipliers[i];" & vbCrLf
    jsCode = jsCode & "  total = 11 - total % 11;" & vbCrLf
    jsCode = jsCode & "  if (total > 9) {total = 0;};" & vbCrLf
    jsCode = jsCode & "  if (total == vatnumber.slice (7,8))" & vbCrLf
    jsCode = jsCode & "    return true" & vbCrLf
    jsCode = jsCode & "  else" & vbCrLf
    jsCode = jsCode & "   return false;" & vbCrLf
    jsCode = jsCode & "}" & vbCrLf
    jsCode = jsCode & ";" & vbCrLf
    
    With ScriptControl1
        .Language = "Javascript" 'Set script language.
        .AllowUI = False 'Reset UI interaction (TRUE is the default).
        .AddCode jsCode 'Form1.Text1.Text 'Copy the script code to the control.
        Debug.Print "Result =" & .Run("FIVATCheckDigit", "09508951")
    End With
    
    End Sub
    String variable method works ok.

    Any idea how to debug jscript code - preferably when running msscript code?
    Tried to adapt
    Code:
    .addobject "txteditor" text1
    method, but this does not work.
    Last edited by Tech99; Jul 18th, 2016 at 07:31 AM.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,255

    Re: MsScript - jscript code, run-time error 1014 - invalid character

    Quote Originally Posted by Tech99 View Post
    Attached is the test project.
    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...

    Olaf

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: MsScript - jscript code, run-time error 1014 - invalid character

    Quote Originally Posted by Schmidt View Post
    ... 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).

    Quote Originally Posted by Tech99 View Post
    Any idea how to debug jscript code ...?
    You might also want to check out David Zimmer's OCX: Javascript Engine, debugger, IDE.


    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
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: MsScript - jscript code, run-time error 1014 - invalid character

    Quote Originally Posted by Bonnie West View Post
    I suspect this is how it got there:
    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.
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width