Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Dec 2000
    Location
    Maine
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorting a QueryDef Object

    Version: Access2000 SR-1

    Hello all.

    I was wondering if it is possible to programmatically sort a QueryDef object. That is, I have a base query with no identified sort fields. This query is used in defining a QueryDef in a module function, and depending on a parameter passed to the function, I want to be able to sort the QueryDef records by a particular field. The QueryDef later becomes part of a Recordset (DAO), so if it can be done through the Recordset, that would be acceptable as well.

    Thanks!

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sorting a QueryDef Object

    Hi Bruce,

    The querydef object has a SQL property that contains the query definition. You have to take that and add the ORDER BY clause. Normally a query looks like this:

    SELECT field1, field2,... FROM table1, table2, .... WHERE .... ORDER BY field1, field2,....;

    First you have to check whether there is an order by clause. Use the instr() function.
    If so, get rid of the existing order by (use left$ or mid$ functions) and get rid of the ; at the end of the SQL statement.

    Now add you new ORDER BY clause, close with ; and save the SQL in the SQL property of your querydef.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •