Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Query - sorting - is this possible?

Status
Not open for further replies.

stephenmbell

IS-IT--Management
Jan 7, 2004
109
US
I am in the middle of building a small web based contacts application. Prior to this, my client was using Yahoo for their contacts - but certain security issues have come up.

My question is this..

my contacts table looks somewhat like this..
----------
id
firstname
middle
lastname
homephone
workphone
mobile
fax
other
address
companytitle
jobtitle
companyaddress
notes
----------

And here is where I am stuck... I am using an Access DB for a backend. I want to query the contacts and order them by Lastname, Firstname **BUT - if there is a company name, I want to order by that

example:

Adams, Mike
Andrews, Dave
Apple Store
Bell, Dan
Best Western

is this possible with having all of the data in the same table??

i am currently using
select * from contacts order by lastname, firstname, companytitle
Thanks.
 
You could do this with a UNION query.
Code:
SELECT lastname & firstname AS ContactName
FROM contacts

UNION

SELECT companytitle
FROM contacts


ORDER BY ContactName

This combines the results of two different queries. You must have the same number and type of columns in both SELECT lists; otherwise the queries can retrieve different columns from the same table or from different tables.

See Access Help topic Create an SQL-specific query for more on creating a UNION query.


 
Or just
Code:
SELECT IIf(Len([CompanyName] & "")=0,[LastName] & ", " & [FirstName],[companyName]) AS DispName
FROM tblContacts
ORDER BY IIf(Len([CompanyName] & "")=0,[LastName] & ", " & [FirstName],[companyName]);

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top