How to return ADODB.Recordset from function procedure


I am trying to create the function procedure that retrieves the data from db using ActiveX Data Object LIbrary.
This function returns the value of which type is ADODB.Recordset,but the error occurs in the statement that returns the value.
The code is as follow:

Private Function ExecuteQuery(ByVal query As String) As ADODB.Recordset
On Error GoTo Exception

Dim connection as ADODB.connection
Dim command as ADODB.command : Set command = new command
Set connection = CurrentProject.connection
command.ActiveConnection = connection
command.ActiveConnection.CursorLocation = adUseClient
command.CommandText = query
Set ExecuteQuery = command.Execute()

MsgBox Err.Description
Err.Raise (Err.Number)
Exit Function
End Function

The above code causes the error in the statement:"Set ExecuteQuery = command.Execute()" ,and error number is 5
Any help on this would be appreciated.

0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.5K Reputation points

    Maybe you must add 'Exit Function' after the 'Set ExecuteQuery = command.Execute()' line?

0 additional answers

Sort by: Most helpful