Jump to content

DB query problem


Recommended Posts

I'm using the structure below to connect to an access database, and sometimes the usually successful query crashes the script and says requested action with this object has failed.

I'm trying to get it not to crash my script when this happens...

Global $__oInvConn, $__iInvCount = 0
Global $sDatabase = "C:\mydb.mdb"

Func _InvConn()
    If $__iInvCount = 0 Then
        SetError(1)
    EndIf
    Return $__oInvConn
EndFunc   ;==>_InvConn

;**********************************************************************
; opens the database
; returns true if db was opened successfully
;**********************************************************************
Func _InvOpen()
    If $__iInvCount = 0 Then
        ObjEvent("AutoIt.Error", "__InvCOMErr")
        $__oInvConn = ObjCreate("ADODB.Connection")
        $__oInvConn.Provider = "Microsoft.Jet.OLEDB.4.0"
        $__oInvConn.Open($sDatabase)
        If Not @error Then
            $__iInvCount += 1
            Return True
        Else
            Return False
        EndIf
    Else
        $__iInvCount += 1
        Return True
    EndIf
EndFunc   ;==>_InvOpen

;**********************************************************************
; closes the database
;**********************************************************************
Func _InvClose()
    If $__iInvCount > 0 Then
        $__iInvCount -= 1
        If $__iInvCount = 0 Then
            $__oInvConn.Close
            ObjEvent("AutoIt.Error", "")
        EndIf
    EndIf
EndFunc   ;==>_InvClose

;**********************************************************************
; Returns results of a query into the database
; returns the recordset object itself
; assumes the database is already open
; CursorType: 0 = forward-only recordset
;             2 = updatable recordset
;**********************************************************************
Func _InvQueryRS($sQuery, $iCursorType = 0)
    Local $oRs

    If $__iInvCount = 0 Then
        SetError(2)
    Else
        $oRs = ObjCreate("ADODB.Recordset")
        $oRs.CursorType = $iCursorType; adOpenForwardOnly = 0
        $oRs.LockType = 3; adLockOptimistic  = 3
        $oRs.Open($sQuery, $__oInvConn)
        ;If @error Then
            ;SetError(1)
        ;EndIf
    EndIf
    Return $oRs
EndFunc   ;==>_InvQueryRS

;**********************************************************************
; Returns results of a query into the database
; returns an array of strings
; each string is a character separated list of fields returned by the query.
; the default separator character is the comma
; as usual, element 0 is count, like stringsplit
;**********************************************************************
Func _InvQuery($sQuery, $sSeparator)
    Local $oRs, $aResults[1] = [0], $sRecords
    Local Const $adClipString = 2

    If _InvOpen() Then
        $oRs = _InvQueryRS($sQuery)
        If Not @error Then
            $sRecords = $oRs.GetString($adClipString, $oRs.RecordCount, $sSeparator, @CR, '')
            While StringLen($sRecords) > 0 And StringRight($sRecords, 1) = @CR
                $sRecords = StringTrimRight($sRecords, 1)
            WEnd
            $aResults = StringSplit($sRecords, @CR)
            $oRs.Close
        EndIf
        _InvClose()
    EndIf
    Return $aResults
EndFunc   ;==>_InvQuery

;**********************************************************************
Func __InvCOMErr()
    Local Const $oCOMError = @COM_EventObj
    Local $sHexNum = Hex($oCOMError.Number, 8)
    MsgBox(0, @ScriptName, "We intercepted a COM Error !" & @CRLF & _
            "On line " & $oCOMError.scriptline & " of the script." & @CRLF & _
            "Number is: " & $sHexNum & @CRLF & _
            "Windescription is: " & $oCOMError.WinDescription, 10)
    SetError(1)
EndFunc   ;==>__InvCOMErr
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...