Jump to content

$obj.Open("xxx") doesn't set @error ?


FMS
 Share

Recommended Posts

Hello,

I'm not sure where to ask this but :
I'm busy whit an project that needs a SQL connection whit a database.
The problem I have is in the code below: (It works when i fill in the correct info or there is a connection)

 

$conn = ObjCreate( "ADODB.Connection" )
   $DSN = "DRIVER={SQL Server};SERVER=someservername;DATABASE=dbname;UID=user;PWD=pass;"
   
   $conn.Open($DSN)
   If @error Then
       ConsoleWrite("ObjOpen Failed.")
   Else
       ConsoleWrite("ObjOpen Success.")
   EndIf

Is the code faulty or the @error isn't set this way iff there is no connection?
When i search on this forum 9/10 likewise problems are refered to catsh the @error this way but I get the error below :D 
 

*location*==> The requested action with this object has failed.:
$conn.Open($DSN)
$conn^ ERROR

 

as finishing touch god created the dutch

Link to comment
Share on other sites

 

 

1 hour ago, FMS said:

When i search on this forum 9/10 likewise problems are refered to catsh the @error this way but I get the error below

I bet those 9/10 had COM error handlers defined.

In order to be able to process @error after the COM statement, you need to have a COM error handler.  Lookup the ObjEvent() function in the Help File, especially example 2 if you just want to be able to process @error.

Link to comment
Share on other sites

Thanks @TheXman, found it and checked. I'm rather new to object handling.
For futher reference (or maybe I've implemented it wrong) here is a simplefied piece of code I'll use :)
 

Global $err_handler = 0

Func Err_UnRegister()
   $err_handler = 0
EndFunc

Func Err_Register()
   $err_handler = ObjEvent("AutoIt.Error", "err_func")
EndFunc

Func err_func()
   ConsoleWrite("ObjOpen Failed."& @CRLF )
EndFunc

Func set()
   $conn = ObjCreate( "ADODB.Connection" )
   $DSN = "xxx"
   Err_Register()
   $conn.Open($DSN)
   If @error Then MsgBox(4096, "COM Error Detected", @error)
   Err_UnRegister()
EndFunc


set()

 

as finishing touch god created the dutch

Link to comment
Share on other sites

You're welcome! :thumbsup:

 

You could simplify it by doing something like this:

set()

Func set()
    ;Set COM error handler
    $oComErr = ObjEvent("AutoIT.Error", "com_error_handler")

    $conn = ObjCreate( "ADODB.Connection" )
    $DSN = "xxx"
    $conn.Open($DSN)
    If @error Then MsgBox(4096, "COM Error Detected", $oComErr.description)
EndFunc

Func com_error_handler($oError)
    Return
EndFunc

 

Edited by TheXman
Link to comment
Share on other sites

When working with ADO you should have a look at the ADO UDF which can be found on the wiki.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

I think the ADO UDF provides a better way to create/open a connection as the error handling is built in ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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...