ERROR: Operation is not allowed when the object is closed

nithinkm

Board Regular
Joined
May 7, 2008
Messages
70
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 :mad: ). 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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Check if RecSet Is Nothing before you try and access its EOF or BOF properties.
 
Upvote 0
THanks for the riply

I have checked for nothing, but the problem remains the same.
i have checked for nothing as --->

If not RecSet is nothing Then
---code here----
End If

Is this the correct way to check record set for nothing

regards
Nithin
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top