Results 1 to 5 of 5

Thread: small problem

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Location
    israel
    Posts
    81

    small problem

    i cannot figure out why the RS is not recognized in the catch area.
    code:
    please try to help

    Public Function IsDataExist(ByVal Table As String, ByVal Field As String, ByVal value As String, Optional ByVal Operand As String = " = ") As Boolean
    Try
    Dim RS As New ADODB.Recordset()

    RS.Open("SELECT COUNT(" & Field & ") AS CNT FROM " & Table & " WHERE " & Field & Operand & value, ConCourses, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)

    IsDataExist = CBool(Val(RS("CNT")) & "")

    If RS.State Then RS.Close()
    RS = Nothing
    Exit_Function:
    Exit Function

    Catch
    MessageBox.Show("IsDataExist" & vbNewLine & Err.Description & vbNewLine & "FROM " & Table & " WHERE " & Field & Operand & value & vbNewLine & Err.Number)
    If RS.State Then RS.Close()
    RS = Nothing
    IsDataExist = False
    End Try
    End Function

  2. #2
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    declare the RS outside of the try catch sequence. Declaring it within the try means it goes out of scope when it reaches catch.

  3. #3
    Lively Member
    Join Date
    Feb 2000
    Posts
    120
    I underStand that but i dont want to declare a new recordset i just want to kill the old one if i had an error .

    10x
    shachar shaty

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I think he means move it:
    VB Code:
    1. Public Function IsDataExist(ByVal Table As String, ByVal Field As String, ByVal value As String, Optional ByVal Operand As String = " = ") As Boolean
    2. Dim RS as ADODB.Recordset
    3. Try
    4. RS=New ADODB.Recordset()
    5.  
    6. RS.Open("SELECT COUNT(" & Field & ") AS CNT FROM " & Table & " WHERE " & Field & Operand & value, ConCourses, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
    7.  
    8. IsDataExist = CBool(Val(RS("CNT")) & "")
    9.  
    10. If RS.State Then RS.Close()
    11. RS = Nothing
    12. Exit_Function:
    13. Exit Function
    14.  
    15. Catch
    16. MessageBox.Show("IsDataExist" & vbNewLine & Err.Description & vbNewLine & "FROM " & Table & " WHERE " & Field & Operand & value & vbNewLine & Err.Number)
    17. If RS.State Then RS.Close()
    18. RS = Nothing
    19. IsDataExist = False
    20. End Try
    21. End Function

    If its dimmed inside the Try then the scope is limited to just the Try part. You have to dim it outside of that so the scope covers the Catch and Finally parts as well.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Code:
            Dim RS As New ADODB.Recordset()
            Try
                RS.Open("SELECT COUNT(" & Field & ") AS CNT FROM " & Table & " WHERE " & Field & Operand & value, ConCourses, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly)
    Like Musician says, what's wrong with declaring the recordset outside the try block? The RS.Close/RS = Nothing will still process correctly if it gets run, and besides the recordset is going bye bye anyhow when the function exits... ?

    ...
    dam j00 edneeis, u r 2 fast 4 me!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width