Jump to content
Sign in to follow this  

Access Database Connection

Recommended Posts


I am a newbie and have tried connecting to a Access database on my desktop. I have used the below with no luck, I get the following error:

Line 24: (File "C:\Documents and Settings etc.

$adoRs.Open ($query, $adoCon)

$adoRs.Open ($query, $adoCon)^ERROR

Error: The requested acction with this object has failed.

$dbname = "C:\Documents and Settings\DOHERTYW\Desktop\NHS Status.mdb"

$tblname = "TBL_LowerCase"

$query = "SELECT * FROM " & $tblname & " WHERE DistrictNumber = 1"

Local $title

$adoCon = ObjCreate("ADODB.Connection")

$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $dbname) ;Use this line if using MS Access 2003 and lower

;$adoCon.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $dbname) ;Use this line if using MS Access 2007 and using the .accdb file extension

$adoRs = ObjCreate ("ADODB.Recordset")

$adoRs.CursorType = 1

$adoRs.LockType = 3

$adoRs.Open ($query, $adoCon)

$title = $adoRs.Fields("DistrictNumber").value ;Retrieve value by field name

;$title = $adoRs.Fields(2).value ;Retrieve value by column number



Share this post

Link to post
Share on other sites

I don't see you testing if .Open was successful on your connection before moving on. Also, a COM Error handler should be included anytime you use COM objects in AutoIt:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler

$adStateClosed = 0x0 ; The object is closed
$adStateOpen = 0x1 ; The object is open
$adStateConnecting = 0x2 ; The object is connecting
$adStateExecuting = 0x4 ; The object is executing a command
$adStateFetching = 0x8 ; The rows of the object are being retrieved

; ...

$adoCon.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & $dbname)
$iADOState = $adoCon.State
If $iADOState = $adStateOpen Then
    MsgBox(64, "Success", "Connection object is open.")
    MsgBox(16, "Error", "Connection is not open; $adoCon.State = " & $iADOState)

; ...

; This is my custom error handler
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    MsgBox(16, "COM Error", "We intercepted a COM Error!" & @CRLF & _
            "Number is: " & $HexNumber & @CRLF & _
            "Windescription is: " & $oMyError.windescription)
EndFunc   ;==>MyErrFunc


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...