Search code examples
ms-accessms-access-2003recordset

Query a recordset object using VBA


I'm using Access 2003 and have a form that allows the user to pick from a variety of filters, and I use VBA to update the recordset of a subform based on those filters (I generate a SQL statement in VBA). This subform can have duplicate client ids, and now I'm trying to get to a unique list of client ids.

Is there any easy way to query out the unique client ids using VBA if I have the source SQL for the subform? I've thought of these options:

  • Write all the ids to a temp table and then query that table (seems like more work/resources than are necessary)
  • Somehow apply a query to a recordset object in VBA (is this possible?); I'd set the recordset object equal to the SQL query and then try to run a SELECT DISTINCT client_id FROM <the recordset object>, but I'm not finding any information that leads me to believe this is possible
  • Generate a new SQL query based on the original one (I was hoping SELECT DISTINCT client_id FROM ('original select query text here') would work, but it gave me a syntax error in the FROM statement

Solution

  • Aim for the third alternative. This should work if you alias the subquery, and 'original select query text here' can fit.

    SELECT DISTINCT sub.client_id
    FROM
        (
            'original select query text here'
        ) AS sub
    

    If Access still chokes, show us what you have for 'original select query text here'.

    You're right about alternative #1 ... that is wasteful.

    Alternative #2 is not possible because Access won't let you run a query using a recordset object as the FROM source.