Most of you probably already know this but I think it’s worth mentioning. Sometimes it’s necessary to provide certain mechanism in VCP template that will inform template user that values he is inputting are wrong or out of range. Those mechanisms are quite easy to implement in basic form and will make your templates more “fool proof”.

Where to start? Ok, so let’s make a quick example of a template with simple validation mechanisms in the back. Lets say we have some text fields. We expect user to input percent values in them. Values should be greater than zero and can fractional part. First thing we should do is naming those text fields. In this example I named them txtResult1, txtResult2, … and so on. (see Good habits ). What we are going to do is to handle OnChange event of text field. Since validation mechanism will be the same for all textboxes we’re going to write only one implementation of event handler and assign it to all textboxes.

Sub txtResultChange(Sender)

End Sub

Ok, now we have this one we should think of a way to inform user about the result of validation. Most often I just change the back color of textbox to “red” when inputed value is of wrong type or is missing, “yellow” when value is out of range and I leave default backcolor when everything is fine. Here’s how I would implement this:

Sub txtResultChange(Sender)
  Dim val

  If (Sender.UTF8Text = "") Then
    ' missing value (textbox is empty)
    Sender.Color = clRed
  Else
    val = ConvertToDouble(Sender.UTF8Text)
    If IsNull(val) Then
      ' value is not valid number
      Sender.Color = clRed
    Else
      ' value is valid number ...
      If (val <= 0) Then
        ' ... but it's out of range
        Sender.Color = clYellow
      Else
        ' ... and it's positive
        Sender.Color = clWindow
      End If
    End If
  End If

End Sub

And here that's it :-). But as you may have noticed we are using some ConvertToDouble function which probably converts text values to double. That's exactly what it does, but before I provide you the code for this one we should talk about error handling.

Error handling

When there's a little time to prepare a template this is the part most of us would skip. Sometimes lack of error handling will do no harm, but sometimes could really put us in missery. When error occurs and it's not properly handled all we can do is close opened templated and try to fill it again. Unfortunatelly in some rare cases there you may have to close VCP because it stops responding. To prevent this we should always when we can implement error handling routines. But how can we do this?

In previous section of this article I used a function that converts text value to double. You may have wondered why I didn't used just CDbl function. Well the answer is simple. CDbl returns error when provided value is not a valid number. So how can we handle those errors? In VBScript to handle errors we should use On Error statement. We can then examine the Err error object and analyze errors at any time. Error handling in VBScript has two elements that work together. We can turn it on with the On Error Resume Next statement and turn it off with On Error GoTo 0. When error handling is on we gain access to Err error object. This object has three properties that are generally usefull:

  • Number - (the default property) contains integer value. If this value is 0 it means no error occured.
  • Source - string value indicating source of an error
  • Description - string value, textual description of an error

It also provides two methods:

  • Clear
  • Raise(lngNumber, strSource, strDescription)

Clear takes no parameters. It simply clears the values of all the properties of the previous error. It's very important to use Clear after each time you check Err. Otherwise, the information from the previous error will persist in the Err object and if you check again but no intervening error has occurred, the same error information will still be there and you may get a false positive on your error check. With the Raise method you can create a VBScript error.

Here's how I implemented error handling in my ConvertToDouble function:

' Converts String value to Double
' if value cannot be converted - returns Null
Function ConvertToDouble(val)
  On Error Resume Next

  Dim result, tmpVal

  result = CDbl(val)

  If (Err.Number <> 0) Then
     ' value cannot be converted
     Err.Clear
     ' replace dot with coma and try again
     tmpVal = Replace(val, ".", ",")
     result = CDbl(tmpVal)
     If (Err.Number <> 0) Then
        ' value still cannot be converted
        Err.Clear
        ' replace coma with dot and try once again
        tmpVal = Replace(val, ",", ".")
        result = CDbl(tmpVal)
        If (Err.Number <> 0) Then
           ' value still cannot be converted
           ' probably not a valid number
           Err.Clear
           result = Null
        End If
     End If
  End If
  ConvertToDouble = result
End Function

And so it's basically it. If you have any ideas or suggestions regarding error-handling in VCP, please don't hasitate to post a comment here. I would really like to know your oppinions and experiances.

GHTime Code(s): 7e321