Jump to content

SQL & @error


Recommended Posts

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! :)

Link to comment
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
Link to comment
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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...