FMS Posted October 16, 2019 Posted October 16, 2019 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 *location*==> The requested action with this object has failed.: $conn.Open($DSN) $conn^ ERROR as finishing touch god created the dutch
TheXman Posted October 16, 2019 Posted October 16, 2019 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. FMS 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
FMS Posted October 16, 2019 Author Posted October 16, 2019 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
TheXman Posted October 16, 2019 Posted October 16, 2019 (edited) You're welcome! 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 October 16, 2019 by TheXman FMS 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman
water Posted October 16, 2019 Posted October 16, 2019 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
FMS Posted October 16, 2019 Author Posted October 16, 2019 @water I use the ADO.UDF Do you mean that there is a better solution than the simplefied code above? as finishing touch god created the dutch
water Posted October 16, 2019 Posted October 16, 2019 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now