Updating the VBScript Routine

After making changes to the VBScript code in the Code View tab, the user can click the Apply button to save the changes. This will trigger the code shown in Listing 13.7.

Listing 13.7:  Set_Click Subroutine in Programmable Calculator

Private Sub Set_Click()
Dim i As Integer
Dim k As Integer
On Error Resume Next
If Toolbar1.Buttons(“EditCode”).Value <> tbrPressed Then
   Exit Sub
End If
k = CInt(KeyPos.Text)
If Err.Number = 0 Then
   If k >= 0 And k <= MaxButtons - 1 Then
      ScriptControl1.AddCode CodeBlock.Text & vbCrLf
      If ScriptControl1.Error.Number = 0 Then
         Code(k) = CodeBlock.Text
         i = InStr(4, CodeBlock.Text, “()”, vbTextCompare)
         CodeName(k) = Mid(CodeBlock.Text, 5, i - 5)
         KeyButton(k).Caption = CodeNam.Text
         SSTab1.Tab = 0
      End If
   End If
End If
End Sub

I begin this routine by using the On Error Resume Next statement to prevent runtime errors from crashing my program. Next, I put in a safety check that ensures that I’m in edit mode when I attempt to save the changes, since it is possible to switch to this display without being in edit mode.

The real meat of this routine begins by converting the value in the button number field (KeyPos.Text) to the variable k. Then I verify that the data is valid by checking the Number property of the Err object and checking to see if it contains a valid KeyButton index.

If everything is correct so far, I use the Add method to add the code to the ScriptControl. If there are no syntax errors, I save the code into the Code array and extract the name of the subroutine, then save it in the CodeName array. Next, I update the caption displayed on the button from the CodeNam.Text field. Finally, I switch back to the Keyboard View by setting the SSTab1.Tab property to 0.

Note that I don’t have any code to handle the error condition. That’s because the code in the ScriptControl’s Error event will take care of displaying the error message and positioning the cursor at the place where the error was detected. This handling is described next.

© 1998 SYBEX Inc. All rights reserved.