Solved

ORDER BY [--] Conflicts with DISTINCT

Posted on 2001-06-26
3
204 Views
Last Modified: 2010-05-02
I have a VB6 program with an ADO connection to Access97 database.  A query that I set up in Access (and ran successfully) was copied/pasted to my VB program.  However, when I try to open a recordset based on the SQL I get the error: ORDER BY [fld] Conflicts with DISTINCT.

The SQL:

    strSelect = "SELECT DISTINCT tblPlacementInfo.IDLink, [LastName] & ', ' & [FirstName] & ' ' & [MiddleName] AS Name, "
    strSelect = strSelect & "tblMainInfo.DateofBirth, LUtblRace.Race, LUtblSex.Sex "
    strSelect = strSelect & "FROM ((tblMainInfo INNER JOIN tblPlacementInfo ON tblMainInfo.LinkID = tblPlacementInfo.IDLink) "
    strSelect = strSelect & "INNER JOIN LUtblRace ON tblMainInfo.Race = LUtblRace.RaceCode) "
    strSelect = strSelect & "INNER JOIN LUtblSex ON tblMainInfo.Sex = LUtblSex.SexCode "
    strSelect = strSelect & "ORDER BY [LastName], [FirstName], [MiddleName] ;"

I suspect someone's encountered this somewhere else, but I did not find any refs.

Thanks.

Ken
0
Comment
Question by:kenspencer
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
Comment Utility

   strSelect = "SELECT DISTINCT tblPlacementInfo.IDLink, [LastName] & ', ' & [FirstName] & ' ' & [MiddleName]
AS Name, "
   strSelect = strSelect & "tblMainInfo.DateofBirth, LUtblRace.Race, LUtblSex.Sex "
   strSelect = strSelect & "FROM ((tblMainInfo INNER JOIN tblPlacementInfo ON tblMainInfo.LinkID =
tblPlacementInfo.IDLink) "
   strSelect = strSelect & "INNER JOIN LUtblRace ON tblMainInfo.Race = LUtblRace.RaceCode) "
   strSelect = strSelect & "INNER JOIN LUtblSex ON tblMainInfo.Sex = LUtblSex.SexCode "
   strSelect = strSelect & "ORDER BY [LastName] & ', ' & [FirstName] & ' ' & [MiddleName]"

Should work for you.
0
 
LVL 6

Expert Comment

by:andyclap
Comment Utility
Unfortunately this is the way it works in access. If you use an orderby after a distinct you'll need to select the fields you're ordering by too. You can use a calclated field, but annoyingly you need to use the calculation again rather than the alias to reference it in the order by

You can either use:

strSelect = "SELECT DISTINCT tblPlacementInfo.IDLink, [LastName] & ', ' & [FirstName] & ' ' & [MiddleName]
AS Name, "
   strSelect = strSelect & "tblMainInfo.DateofBirth, LUtblRace.Race, LUtblSex.Sex "
   strSelect = strSelect & "FROM ((tblMainInfo INNER JOIN tblPlacementInfo ON tblMainInfo.LinkID =
tblPlacementInfo.IDLink) "
   strSelect = strSelect & "INNER JOIN LUtblRace ON tblMainInfo.Race = LUtblRace.RaceCode) "
   strSelect = strSelect & "INNER JOIN LUtblSex ON tblMainInfo.Sex = LUtblSex.SexCode "
   'order by the calculated field
   strSelect = strSelect & "ORDER BY [LastName] & ', ' & [FirstName] & ' ' & [MiddleName] ;"

or

strSelect = "SELECT DISTINCT tblPlacementInfo.IDLink, [LastName] & ', ' & [FirstName] & ' ' & [MiddleName]
AS Name, "
   strSelect = strSelect & "tblMainInfo.DateofBirth, LUtblRace.Race, LUtblSex.Sex "
  'have to bring back these fields too
   strSelect = strSelect & ", [LastName], [FirstName], [MiddleName] "
   strSelect = strSelect & "FROM ((tblMainInfo INNER JOIN tblPlacementInfo ON tblMainInfo.LinkID =
tblPlacementInfo.IDLink) "
   strSelect = strSelect & "INNER JOIN LUtblRace ON tblMainInfo.Race = LUtblRace.RaceCode) "
   strSelect = strSelect & "INNER JOIN LUtblSex ON tblMainInfo.Sex = LUtblSex.SexCode "
   strSelect = strSelect & "ORDER BY [LastName], [FirstName], [MiddleName] ;"

0
 
LVL 3

Author Comment

by:kenspencer
Comment Utility
Oops!  I had copied/pasted the SQL, then edited it to remove the double-quotes from the calc field (Name), and that's where I took out the ", " and " " entirely.  I did not realize that DISTINCT causes problems with ORDER BY.  Tim was first and so gets the points.  Thanks to Andy for the explanation.

Ken
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now