In Figure 13.4, you see the results of evaluating a simple mathematical expression. Figure 13.5 shows that strings and functions can be included in the expression.
Figure 13.4: Evaluating a simple expression
Figure 13.5: Strings can be evaluated also.
NOTE: Expressions need formatting, too: Expressions passed to the Eval method for processing must conform to the standard syntax rules for Visual Basic. The expression must fit on a single line, or each line but the last line must end with a blank followed by the underscore character.
The code in Listing 13.2 runs when the user clicks the Eval button. I use the On Error Resume Next statement to prevent runtime errors from occurring when I evaluate an expression. Then I simply call the Eval method and format the results. I used the Format function rather than one of the newer functions because it will format nearly any value; FormatNumber, FormatDateTime, and the rest require specific data types.
Listing 13.2: Command5_Click Event in MSScript Demo
Private Sub Command5_Click()
On Error Resume Next
MsgBox Format(ScriptControl1.Eval(Text1.Text))
End Sub