Solved

ORDER BY [--] Conflicts with DISTINCT

Posted on 2001-06-26
3
205 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
ID: 6227856

   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
ID: 6227950
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
ID: 6228331
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

914 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

21 Experts available now in Live!

Get 1:1 Help Now