SQL code help in excel vba

lpking2005

Board Regular
Joined
Mar 21, 2011
Messages
140
Hi,

Im not sure if this belongs in Excel or Access forum section.


I have the following sql code which is working lovely but i need to modify it.
Code:
sSql = "SELECT * FROM " & i & " WHERE [Pick Status] = 'Ordered'"

So obviously it looks at a table and retrieves only records that have a status of "Ordered" and places them one by one into an excel sheet.

But recently i had to change the table and add an 'Order Priority' column.
So every order entered into the database now has a priority number of 2 (low priority) but if the user adds a high priority order, the number will be 1.

What i need is to still select the orders that have the status 'Ordered' but also check to see if the 'Order Priority' number is set as 1 and select that first before moving onto records with a number 2 priority.

I hope this isnt confusing!?

Any help will be very much appreciated.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Is the [Order Priority] field already getting pulled then? It sounds like you just want to add an "Order By" statement so that the 1's are sorted towards the top.

Try:
Code:
sSql = "SELECT * FROM " & i & " WHERE [Pick Status] = 'Ordered' Order by [Order Priority]"
 
Upvote 0
Thank you for the reply!

That makes sense but it is still not selecting the Priority 1 order first or not at least putting it at the top of the list in excel sheet.
 
Upvote 0
i have just worked it out!

I have left the sql statement the same and instead changed the record sort code: rst.sort[ID] to rst.sort[OrderPriority]

It seems to do the trick. :)

Thank you anyway!
 
Upvote 0

Forum statistics

Threads
1,215,743
Messages
6,126,608
Members
449,321
Latest member
syzer

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