Jump to content

SQL Server Error


XinLu
 Share

Recommended Posts

For example, this is what you will see in my database

Table: dbo.fm_layout_branches

ids branch_txt

01 CS

02 sth else

when I run the following code, NO errors occured, everything works fine

$Connection = ObjCreate("ADODB.Connection")

$RecordSet=ObjCreate("ADODB.Recordset")

$string = "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;Initial Catalog=test;User ID=test;Password=test;"

$Connection.Open($string)

$RecordSet.Open("Select * FROM dbo.fm_layout_branches WHERE branch_txt = 'CS';", $Connection)

MsgBox(0, Default, $RecordSet.Fields("ids").value)

However, if I try something are not existing in the table, error occurs, for example

$RecordSet.Open("Select * FROM dbo.fm_layout_branches WHERE branch_txt = 'xxxx';", $Connection)

The error is

Line -1:

Error: The requested action with this object has failed.

My question will be: is there anyway I can know from AutoIt there is NO record matching the SELECT FROM WHERE condition, and meanwhile, keep the program running (right now, it quits immediately)

Thanks a lot!

Edited by XinLu
Link to comment
Share on other sites

Link to comment
Share on other sites

@xinlu

Maybe you can usesomething like @error

If @error Then
    MsgBox(4096,"Error", "No Records Found")
Else
    MsgBox(4096, "Result", $result)
EndIf

Regards,

ptrex

Thank you for your reply. It won't work that way, because the error teminates the script immediately. @error won't be detected.

However, I actually figured how to solve it. I used ObjEvent()

$Err=ObjEvent("AutoIt.Error","ODBCJET_ErroHandler")

Func ODBCJET_ErroHandler()
    MsgBox(262192, _
    "Data Exchange Error", _
    "Error number: " & $Err.Number & @CRLF & _
    "Error source: " & $Err.Source & @CRLF & _
    "Description: " & $Err.Description)
    Local $err1 = $Err.number
    If $err1 = 0 Then $err1 = -1
    SetError($Err)  
EndFunc

To gather if there is at least one record in the table matching up the condition, I used this:

$RecordSet.Open("Select * FROM dbo.fm_inventory WHERE part = '" & $part & "'", $Connection)
MsgBox(0, Default, $RecordSet.EOF)

EOF always returns -1 if there is NO records found.

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