|
-
Oct 5th, 2002, 01:08 PM
#1
Thread Starter
Registered User
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
-
Oct 5th, 2002, 02:46 PM
#2
Hyperactive Member
declare the RS outside of the try catch sequence. Declaring it within the try means it goes out of scope when it reaches catch.
-
Oct 6th, 2002, 02:04 AM
#3
Lively Member
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
-
Oct 6th, 2002, 02:10 AM
#4
I think he means move it:
VB Code:
Public Function IsDataExist(ByVal Table As String, ByVal Field As String, ByVal value As String, Optional ByVal Operand As String = " = ") As Boolean
Dim RS as ADODB.Recordset
Try
RS=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
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.
-
Oct 6th, 2002, 02:11 AM
#5
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|