Hi
Im using code#1 for getting connection to the datadase and retrieve recordset(RecSet). It works fine ,But when the connection is not there A run time error (Operation is not allowed when the object is closed)shows in code#2, from where code#1 is being called (marked as "---->>")
I have googled many times and tried all the codes to check for empty record set (I think the problem is due to that
). But my problem is not yet solved.
Please help me in doing this.
-----------------------------------------------------------------------
code #1
---------
Public Function GetItemCodeConnection(ByVal sqlqry As String) As ADODB.Recordset
On Error GoTo Cleanup
Set ConOra = New ADODB.Connection
If ConOra.State = ObjectStateEnum.adStateOpen Then
ConOra.Close
End If
ConString = "myconnectiomstring"
ConOra.ConnectionString = ConString
ConOra.Open
Dim ra As Long
Set RecSet = Nothing
Set RecSet = ConOra.Execute(sqlqry, ra, CommandTypeEnum.adCmdText)
Set GetItemCodeConnection = RecSet
Exit Function
Application.ScreenUpdating = False
Cleanup:
Debug.Print Err.Description
Set RecSet = Nothing
If ConOra.State = ObjectStateEnum.adStateOpen Then
ConOra.Close
End If
Worksheets("Sheet1").Range("ExitVariable").value = 0
End Function
==============================
code#2
-------
Public Function GetMatchQuery(ValueString As String, TableString As String, FieldString As String) As Integer
MatchString = "Select " & FieldString & " from " & TableString & " where " & FieldString & "='" & ValueString & "'"
Set RecSet = Nothing
Set RecSet = GetItemCodeConnection(MatchString)
---->>If RecSet.EOF And RecSet.BOF Then
MatchResult = 0
Else
MatchResult = 1
End If
GetMatchQuery = MatchResult
End Function
Im using code#1 for getting connection to the datadase and retrieve recordset(RecSet). It works fine ,But when the connection is not there A run time error (Operation is not allowed when the object is closed)shows in code#2, from where code#1 is being called (marked as "---->>")
I have googled many times and tried all the codes to check for empty record set (I think the problem is due to that
Please help me in doing this.
-----------------------------------------------------------------------
code #1
---------
Public Function GetItemCodeConnection(ByVal sqlqry As String) As ADODB.Recordset
On Error GoTo Cleanup
Set ConOra = New ADODB.Connection
If ConOra.State = ObjectStateEnum.adStateOpen Then
ConOra.Close
End If
ConString = "myconnectiomstring"
ConOra.ConnectionString = ConString
ConOra.Open
Dim ra As Long
Set RecSet = Nothing
Set RecSet = ConOra.Execute(sqlqry, ra, CommandTypeEnum.adCmdText)
Set GetItemCodeConnection = RecSet
Exit Function
Application.ScreenUpdating = False
Cleanup:
Debug.Print Err.Description
Set RecSet = Nothing
If ConOra.State = ObjectStateEnum.adStateOpen Then
ConOra.Close
End If
Worksheets("Sheet1").Range("ExitVariable").value = 0
End Function
==============================
code#2
-------
Public Function GetMatchQuery(ValueString As String, TableString As String, FieldString As String) As Integer
MatchString = "Select " & FieldString & " from " & TableString & " where " & FieldString & "='" & ValueString & "'"
Set RecSet = Nothing
Set RecSet = GetItemCodeConnection(MatchString)
---->>If RecSet.EOF And RecSet.BOF Then
MatchResult = 0
Else
MatchResult = 1
End If
GetMatchQuery = MatchResult
End Function