Query - Sort by Search Criteria

r055c

New Member
Joined
Feb 24, 2009
Messages
30
Hi all,

I have a problem with a access project I am working on where i have a single table (Table A) with 5 fields in it.

I am running a query (using query builder) to pull back data from (Table A), for example 1,5,2,4 (not alphabetical or ascending/decending. The query works fine, however I would expect the results to be in the same order as the cirtiera however it appears to be random.

Is there anyway I could force this show the results in the order specified in the criteria?

thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
SELECT Field_Description.Field_Name, Field_Description.Mandatory, Field_Description.Description, Field_Description.Format_Example, Field_Description.[Useful Information]
FROM Field_Description
WHERE (((Field_Description.Field_Name)="1" Or (Field_Description.Field_Name)="2" Or (Field_Description.Field_Name)="3" Or (Field_Description.Field_Name)="4" Or (Field_Description.Field_Name)="5" Or (Field_Description.Field_Name)="6" Or (Field_Description.Field_Name)="7"));

I would expect the above code to bring back the results in the order i entered them in the criteria (1,2,3,4,5,6,7) when instead it appears random (4,3,1,2,6,7,5)

**for compant reasons i've had to change the field names to numbers but i don't think this would be a problem

thanks

Thanks
 
Upvote 0
Thats not how SQL works if there isn't an explicit order it will use the implicit of the first underlying table.
The full WHERE clause is used record by record... NOT scan all data for the first condition, then scan all data for the second condition ..etc

Code:
SELECT Field_Description.Field_Name, Field_Description.Mandatory, Field_Description.Description, Field_Description.Format_Example, Field_Description.[Useful Information] FROM Field_Description
WHERE (((Field_Description.Field_Name)>="1" and (Field_Description.Field_Name)<="7"  [COLOR=red]ORDER BY ASC Field_Description.Field_Name[/COLOR];
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top