Cách viết hàm trả về Recordset DAO

Public Function getRecs(sql As String) As DAO.Recordset
' Khai bao bien
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String
' Bay loi
On Error GoTo ErrorHandler
 Set dbs = CurrentDb
 Set rst = dbs.OpenRecordset(
sql , dbOpenDynaset)
 Set getRecs = rst
 Exit Function
 ''''''''''''''''''''''''''''''''
ErrorHandler:
   MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description

End Function


'Su dung ham
Private Sub Command1_Click()

  Dim getRS As DAO.Recordset
  Set getRS = getRecs("Select * From tbTest")
  getRS.MoveFirst
  MsgBox getRS!ID
 
  ' Trong Function neu Close thi ham su dung se bi LOI khi lay bang ghi
  getRS.Close
  Set getRS = Nothing
   
End Sub