Customization example: Inserting data into a database

Below is sample code for an external event server to insert data into a database. The code is written in VB5 and uses Microsoft's database access objects (DAO) to access to the Microsoft Access database. The object is an external event server, and it supports a handler for the InvoiceInterpreted event, which is called after an invoice has been interpreted. The handler iterates over all the fields in the invoice, extracts data, and inserts it into the database.

For this code example to work, you must use the class name EventServer and name the file EHISample.exe.

'** Private class variables

'** -----------------------

'**

Private wrkJet As Workspace

Private AccessDB As Database

Private FieldObj As Object

Private AccessDBOpened As Boolean

Private EHIApplication As Object

 

'** Called by Kofax ReadSoft Invoices to establish a connection and open the database

'** ===========================================

'**

Public Sub Connect(EHIApp As Object, DBName As String)

 

On Error GoTo ConnectError

Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)

Set AccessDB = wrkJet.OpenDatabase(DBName, True)

Set EHIApplication = EHIApp

If IsObject(AccessDB) Then

AccessDBOpened = True

Set FieldObj = AccessDB.OpenRecordset("Field")

Else

AccessDBOpened = False 'No error handling

End If

 

Set FieldObj = AccessDB.OpenRecordset("Field")

 

Exit Sub

 

ConnectError:

MsgBox "Error opening database: " & Err.Description

 

End Sub

 

'** Inserts data into the "Field" table

'** ===================================

'**

Public Sub Insert(InvoiceName As String, FieldName As String, _

FieldValue As String, FieldStatus As Long)

 

On Error GoTo InsertError

 

If Not AccessDBOpened Then

Exit Sub ' No error handling, just exits

End If

 

If FieldValue = "" Then

FieldValue = "<empty>"

End If

 

FieldObj.AddNew 'Adds an entry to the database

FieldObj.Fields("InvoiceName").Value = InvoiceName

FieldObj.Fields("Name").Value = FieldName

FieldObj.Fields("Value").Value = FieldValue

FieldObj.Fields("Status").Value = FieldStatus

Call FieldObj.Update 'Updates the database, i.e. writes the information to disk

Exit Sub

 

InsertError:

MsgBox "Error inserting data in database : " & Err.Description

 

End Sub

 

'** Closes the database connection

'** ==============================

'**

Public Sub DBClose()

If AccessDBOpened Then

AccessDB.Close

AccessDBOpened = False

End If

End Sub

 

'** This function is called by ReadSoft Invoices as an event handler

'** ==================================================

'**

Public Function InvoiceInterpreted() As Long

 

Dim IField As Object

Dim iLoop As Integer

Dim nFields As Integer

 

nFields = EHIApplication.Invoice.GetNoOfFields()

Set IField = EHIApplication.Invoice.GetFirstField()

For iLoop = 1 To nFields

Call Insert(EHIApplication.Invoice.GetImageFile(), IField.GetName(), _

IField.GetValueStr(), IField.GetStatus())

Set IField = EHIApplication.Invoice.GetNextField()

Next iLoop

 

InvoiceInterpreted = 0 '0 = EV_OK return value to Kofax ReadSoft Invoices

End Function

Related topics

Back to instructions

Overview of example