beautypg.com

Kofax DOKuStar Validation User Manual

Page 36

background image

DOKuStar Validation Programming Manual

Page

32

Private Sub Application_OnProjectLoaded(ByVal App As Application)
Set Amount = App.Project.DataSet.Schema.DocumentTypes("Invoice").FieldTypes("Amount")
End Sub

This subroutine will be called when the project is loaded. The only thing that is done here is that you assign the
variable

Amount

, which you defined above, to the right field type: set to the

FieldType

Amount

on the

DocumentType

Invoice

.

Private Function Amount_OnFieldChanging(ByVal Field As Field, ByVal SubField As Field, ByVal
VerifyEventArgs As VerifyEventArgs) As Boolean

Defining this function enables to catch the

FieldChanging

event for the amount field:

Whenever the user changes the amount on an invoice, this function will be called and gets an object

Field

as the

first parameter, which is the field itself. This object has the type

Field

, which is a notion from the

Data

part of the

DataSet

.

The function is expected to return a boolean value; when returning

False

, the change is aborted, i.e. the old value

(before the user typed his changes) is restored and the focus stays on this field. When returning

True

, the changes

are accepted, and the focus usually moves to the next field (“Usually” means: With respect to filters, see separate
chapter).

Amount_OnFieldChanging = True 'Preset return value

The boolean return value is preset to

True.

If Val(Field.Value) > 1000 Then

Now take the

Field

received as a parameter and determine the field’s value. Since a value of a field is always a

string, the

Val

function must be used to convert to a double integer.

If MsgBox("You entered a value > 1000. Please Confirm.", vbOKCancel) = vbCancel Then
Amount_OnFieldChanging = False 'User clicked cancel, so stay in the field
End If

Open the message box. If the user hits

Cancel

, set the return value to

False

, thus aborting the change and

restoring the old contents.

Before getting more into the details, here is a second way of solving the same problem:

Dim WithEvents ctrl As Controller

Private Sub Application_OnProjectLoaded(ByVal App As Application)
Set ctrl = App.Project.DataSet.Controller
End Sub

Private Function ctrl_OnFieldChanging(ByVal Controller As Controller, ByVal Field As Field, ByVal
SubField As Field, ByVal VerifyEventArgs As VerifyEventArgs) As Boolean
ctrl_OnFieldChanging = True 'Preset return value
If Field.Document.Name = "Invoice" And Field.Name = "Amount" Then
If Val(Field.Value) > 1000 Then
If MsgBox("You entered a value > 1000. Please Confirm.", vbOKCancel) = vbCancel Then
ctrl_OnFieldChanging = False 'User clicked cancel, so stay in the field
End If
End If
End If
End Function

This code does the same as the one above, but with a different approach to the problem. Looking at the differences:

In the first line, a variable of the

Controller

type is defined, and then set to ...

Dataset.Controller

when the

project is loaded. The Controller is a central object that gets events for every field and document. The outcome here
is an event for every field, no matter which document type and field type.