Skip to main content
Kofax

Access an External Database

17347

QAID # 17347 Published

Question / Problem:

Access an External Database

Answer / Solution:

External databases can be accessed via ODBC using MS-ADO. Below is a sample of how to do this in KTM script. However, keep in mind that this is merely invoking the ADO COM object in WinWrap script. Like any other external COM library, the specifics of how it works and whether it works properly depend on the library itself. Full explaination or troubleshooting of ADO or ODBC are far beyond the scope of this sample.

First you need to add a reference to MS-ADO to the script for every class the database is being used with:

  • Open the script window of the appropriate class.
  • Choose "Edit" from the main menu and then "References."
  • Add "Microsoft ActiveX Data Objects 2.8 Library (2.8)" (%CommonProgramFiles%\system\ado\msado15.dll), or something similar.

Add the following code to the project script:

Dim m_DatabaseConnection As ADODB.Connection
' example for accessing a named datasource
Const m_ConnectionString1 As String = "DSN=TEST;UID=test;PWD=test"
' example for using a ODBC driver directly (MySQL)
'Const m_ConnectionString2 As String = _
'  "Provider=MSDASQL;Driver={MySQL ODBC 3.51 Driver};Server=localhost;UID=test;PWD=test;database=test"
' example for using a ODBC driver directly (MS Access)
'Const m_ConnectionString3 as String = _
'  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Filename.mdb;User Id=test;Password=test"
Public Function GetDatabaseConnection() As ADODB.Connection
   If Not m_DatabaseConnection Is Nothing Then
      Set GetDatabaseConnection = m_DatabaseConnection
      Exit Function
   End If
   On Error GoTo ConnectionFailed
   Set m_DatabaseConnection = New ADODB.Connection
   m_DatabaseConnection.ConnectionString = m_ConnectionString1
   m_DatabaseConnection.Open
   Set GetDatabaseConnection = m_DatabaseConnection
   Exit Function
ConnectionFailed:
   Set m_DatabaseConnection = Nothing
   Set GetDatabaseConnection = Nothing
End Function

Add the following code to a class script:

Private Sub Document_AfterExtract(pXDoc As CASCADELib.CscXDocument)
   Dim DatabaseConnection As ADODB.Connection
   Dim SQL As String
   Dim Cmd As ADODB.Command
   Dim Param As ADODB.Parameter
   Dim Recordset As ADODB.Recordset
   Dim i As Integer
   Dim S As String
   Set DatabaseConnection = GetDatabaseConnection
   If DatabaseConnection Is Nothing Then
      ' error
      Exit Sub
   End If
   ' insert simple data
   SQL = "INSERT INTO test (TimeStamp) VALUES('2006-01-01 10:00:00')"
   DatabaseConnection.Execute(SQL)
   ' insert parameterized data
   SQL = "INSERT INTO test (TimeStamp) VALUES(?)"
   Set Cmd = New ADODB.Command
   Set Cmd.ActiveConnection = DatabaseConnection
Cmd.CommandText = SQL
   Set Param = Cmd.CreateParameter
   Param.Type = adDBTimeStamp
   Param.Value = Now
   Cmd.Parameters.Append Param
   Cmd.Execute
   ' retrieve data
   SQL = "SELECT TimeStamp FROM test"
   Set Recordset = DatabaseConnection.Execute(SQL)
   While Not Recordset.EOF
      S = Recordset(0)
      MsgBox "Record #" & CStr(i) & ": " & S
      Recordset.MoveNext
      i=i +1
   Wend
End Sub

Applies To:

Product Version Category
AXPRO 3.0 Script
AXPRO 3.1 Script
AXPRO 3.5 Script
AXPRO 4.0 Script
AXPRO 4.5 Script
AXPRO 5.0 Script
AXPRO 5.5 Script
  • Was this article helpful?