Sign in to follow this  
Followers 0
travish1

SQL & @error

5 posts in this topic

Alright, I'm bashing my head in on this one. Can anyone give me a hint as to why @error isn't catching when an invalid server is provided?

Instead, the script quits and provides this error:

C:\Scripts\EEG Data Migration.au3 (284) : ==> The requested action with this object has failed.:

$sqlCon.Open($sqlConStr)

$sqlCon.Open($sqlConStr)^ ERROR

$sqlConStr = "DRIVER={SQL Server};SERVER=" & $strDbServer & ";DATABASE=MainExamDataDB;uid=TP;pwd=;"
$sqlCon = ObjCreate("ADODB.Connection")
$sqlCon.Open($sqlConStr)
If @error Then
$dbConnectError = MsgBox(48, "Error", "Failed to connect to the database, please verify the server.")
$sqlCon.Close
WinActivate("EEG Data Migration Parameters")
ControlFocus("EEG Data Migration Parameters", "", 5)
Else
queryStudiesToTransfer()
EndIf

Any help would be greatly appreciated! :)

Share this post


Link to post
Share on other sites



travish1,

You can use an error handler to process this kind of error...do a search on it.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Thanks for the tip, kylomas. I had attempted the error handler. This worked well except that it would still throw an EOF error even when the DB connection was successful. The error is listed below.

$oErrorHandler = ObjEvent("AutoIt.Error", "objError")

err.number is: -2147352567

err.windescription:

err.description is: Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

err.source is: ADODB.Recordset

My guess is that I'm getting this error because I haven't run a query; however, I'm hoping to have this as a separate function for establishing the database connection.

;Function to connect to database
Func dbConnect()
$sqlConStr = "DRIVER={SQL Server};SERVER=" & $strDbServer & ";DATABASE=MainExamDataDB;uid=TP;pwd=;"
$sqlCon = ObjCreate("ADODB.Connection")
$oErrorHandler = ObjEvent("AutoIt.Error", "objError")
$sqlCon.Open($sqlConStr)
If @error Then
$dbConnectError = MsgBox(48, "Error", "Failed to connect to the database, please verify the server.")
$sqlCon.Close
WinActivate("EEG Data Migration Parameters")
ControlFocus("EEG Data Migration Parameters", "", 5)
Else
queryStudiesToTransfer()
EndIf
EndFunc

Func objError($oError)
ConsoleWrite("err.number is: " & @TAB & $oError.number & @CRLF & _
"err.windescription:" & @TAB & $oError.windescription & @CRLF & _
"err.description is: " & @TAB & $oError.description & @CRLF & _
"err.source is: " & @TAB & $oError.source & @CRLF & _
"err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
"err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
"err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
"err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
"err.retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
$dbConnectError = MsgBox(48, "Error", "Failed to connect to the database, please verify the server." & @CRLF & @CRLF & "Error: " & $oError.number & @CRLF & "Description: " & $oError.description)
$sqlCon.Close
WinActivate("EEG Data Migration Parameters")
ControlFocus("EEG Data Migration Parameters", "", 5)
EndFunc

Share this post


Link to post
Share on other sites

travish1,

What are you doing in queryStudiesToTransfer()? You should be able to establish the connection anywhere, as long as the entire script has visibility to whatever variables are being used.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

A few tips:

  • Put the ObjEvent statement to grab COM errors at the top of your script to make sure you grab all errors
  • Move those WinActivate("EEG Data Migration Parameters"), queryStudiesToTransfer etc. statemtents out of function DBConnect. Like the name says the function should only connect to the DB
  • No $sqlCon.Close is needed if $sqlCon.Open was not successfull

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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  
Followers 0