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 FormInterpreted event, which is called after a form has been interpreted. The handler iterates over all the fields in the form, extracts data, and inserts it into the database.

'** Class variables

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

'**

Private DB As Object

Private RS As Object

Private wsp As Object

Private DBOpen As Boolean

Private Application As Object

 

'** Called by FORMS to establish a connection and open the database

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

'**

Public Function Connect(EHFApp As Object, DBName As String)

    On Error GoTo LocalError

    Set Application = EHFApp

    MsgBox "Connecting to event server"

    Set DB = DBEngine.OpenDatabase(DBName)

    If IsObject(DB) Then

        DBOpen = True

    Else

        DBOpen = False

    End If

 

    Set RS = DB.OpenRecordSet("Field")

    Exit Function

 

LocalError:

    MsgBox "Error opening database: " & Err.Description

End Function

 

'** Inserts data into the "Field" table

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

'**

Public Function Insert(FormName As String, FieldName As String, _

                        FieldValue As String, FieldStatus As Long)

    If Not DBOpen Then

        Exit Function

    End If

    RS.AddNew

    RS.FormName = FormName

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

    RS.Value = FieldValue

    RS.Status = FieldStatus

    RS.Update

End Function

 

'** Closes the database connection

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

'**

Public Function DBClose()

    If DBOpen Then

        DB.Close

    End If

End Function

 

'** This function is called by FORMS as an event handler

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

'**

Public Function FormInterpreted() As Long

    Dim FField As Object

    Dim i As Integer

    Dim nFields As Integer

    nFields = Application.Form.GetMaxNoOfFormFields()

    For i = 1 To nFields

        If Application.Form.FormFieldExist(i) > 0 then

            Set Ffield = Application.Form.GetFormFieldNo(i)

            Insert Application.Form.GetName(), FField.GetName(), FField.GetValueStr(),

        End if

FField.GetStatus()

    Next i

 

     OnFormInterpreted = 0 ' Return value to FORMS

End Function

Back to instructions

Overview of example