RNJarvis
8/31/2016 - 12:23 PM

Connecting to a SQL Datasource using the Microsoft ActiveX Data Objects 2.8 Library

Connecting to a SQL Datasource using the Microsoft ActiveX Data Objects 2.8 Library


'** Database Connectivity
Global m_DatabaseConnection As ADODB.Connection




Public Function GetADODBDatabaseConnection() As ADODB.Connection
   On Error GoTo ErrorHandler
   OutputDebugString "KfxKTM_GetDatabaseConnection: Fired"

   If Not m_DatabaseConnection Is Nothing = True Then
      Set GetADODBDatabaseConnection = m_DatabaseConnection
      Exit Function
   End If

   Set m_DatabaseConnection = New ADODB.Connection
   m_DatabaseConnection.ConnectionString = Project.ScriptVariables.ItemByName("QA_ExceptionControl_ADODB_ConnectionString").Value
   m_DatabaseConnection.Open
   Set GetADODBDatabaseConnection = m_DatabaseConnection

Exit Function
ErrorHandler:
   Set m_DatabaseConnection = Nothing
   Set GetADODBDatabaseConnection = Nothing
   General_Error_Handler("GetADODBDatabaseConnection", LOG_ERROR, DISPLAY_TO_USER)
End Function

' ** Example use

Private Sub QA_InvoiceDuplicationSQL(ByVal pXDoc As CASCADELib.CscXDocument, dInvoiceDate As Date, sKCDocumentID As String, sInvoiceNumber As String, sDocumentType As String, sSupplierID As String, vTotalAmount As Variant)
   On Error GoTo ErrorHandler
   OutputDebugString "KfxKTM_QA_InvoiceDuplicationSQL: Fired"

   Dim sSQL As String
   Dim DatabaseConnection As ADODB.Connection
   Dim Recordset As ADODB.Recordset
   Dim sTableName As String

   sTableName = Project.ScriptVariables.ItemByName("QA_ExceptionControl_ADODB_Table:Validated").Value
   Set DatabaseConnection = GetADODBDatabaseConnection

   If DatabaseConnection Is Nothing Then
      'Error
      GoTo ErrorHandler
   End If

   ' First, determine if the current Document ID has been processed at all (perhaps this instance of the document has already passed through validation).

   sSQL = "SELECT COUNT(*) FROM  " & sTableName & " WHERE [DocumentID] = '" & sKCDocumentID & "'"
   Set Recordset = DatabaseConnection.Execute(sSQL)

   If Recordset(0).Value = "0" Then
      OutputDebugString "KfxKTM_This document does not currently exist in the Validated table"
      OutputDebugString "KfxKTM_Now checking for a legitimate duplicate using key document values..."

      sSQL = "SELECT COUNT(*) FROM  " & sTableName & " WHERE [InvoiceNumber] = '" & sInvoiceNumber & "' AND [InvoiceDate] = '" & Format(dInvoiceDate, "dddddd") & "'"
      sSQL = sSQL + " AND [DocumentType] = '" & sDocumentType & "' AND [SupplierID] = '" & sSupplierID & "' AND [TotalAmount] = '" & vTotalAmount & "'"

      Set Recordset = DatabaseConnection.Execute(sSQL)

      If Recordset(0).Value = "0" Then
         ' There are no results / a duplicate invoice has not been processed
         ' Log this invoice
         sSQL = "INSERT INTO " & sTableName &  " VALUES('" & sKCDocumentID & "', '" & sInvoiceNumber & "', '" & Format(dInvoiceDate, "dddddd") & "', '" & sDocumentType & "', '" & sSupplierID & "', '" & vTotalAmount & "', '" & Format(Now, "dddddd") & "')"
         Set Recordset = DatabaseConnection.Execute(sSQL)
         Else
            ' There are results (a duplicate invoice has been detected)
            BLA_UpdateExceptionCode(pXDoc, "007", 1)
            OutputDebugString "KfxKTM_Duplicate Invoice Detected"

      End If

   Else
      OutputDebugString "KfxKTM_This document has been previously validated"
      OutputDebugString "KfxKTM_No further action..."


   End If

   'Recordset.Close
   Set Recordset = Nothing

Exit Sub
ErrorHandler:
   General_Error_Handler("QA_InvoiceDuplicationSQL", LOG_ERROR, DISPLAY_TO_USER)
End Sub