How to create a RecordSet in VBA to execute a SELECT query

This simple method creates and returns a Recordset containing the results of the given SQL query.
Note: the caller is in charge to close the recordset and release the resources once done with DB data.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
Public Function ExecQuery(ByVal sql As String) As ADODB.Recordset On Error GoTo ErrorCmd Dim conn As ADODB.Connection 'Init db objects 'Connection is the current DB one If exception_flag Then On Error Resume Next Set conn = CurrentProject.Connection Set ExecQuery = New ADODB.Recordset 'Exec the query Set ExecQuery = CurrentProject.Connection.Execute(sql) Exit_go: Exit Function ErrorCmd: Set ExecQuery = Nothing End Function
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus