|
Question : Microsoft Access Query
|
|
I have the following query in the rowsource of a listbox called "salesmanfilterlist". The query is triggered by a change in a combobox called "filtername", which lists all the salesmen for my company. i didn't have the last line in the query originally (the "order by" part). i added a second combobox called "orderby". This combobox has 2 columns with the second being the bound column. The first column is the readable text version of the second column. In other words, first column would be "Open Date", second column would be the actual field name "open_date". The second column width is set to zero and is simply a list of other field that i might want the list sorted by. They include "open_date", "rating", "probability", "action_Rqd", "Distribution_co". on the "on change" event of both comboboxes, there is a requery of the listbox "salesmanfilterlist". My problem is that the last part that i did.....the second combobox where i can order my list by.....doesn't seem to have any affect. Any ideas as to why this is?
Code Snippet:
1:
2:
3:
4:
|
SELECT keystoneopportunities.[ID], keystoneopportunities.[Opportunity_Title], keystoneopportunities.[Salesman], keystoneopportunities.[open_date], keystoneopportunities.[Distribution_co], keystoneopportunities.[yearly_usage_KWh], keystoneopportunities.[rating], keystoneopportunities.[probability], keystoneopportunities.[action_rqd]
FROM keystoneopportunities
WHERE (((keystoneopportunities.salesman)=[forms]![OpportunityInput].[filterNAME]))
ORDER BY [forms]![OpportunityInput].[orderby];
|
|
|
Answer : Microsoft Access Query
|
|
GTC-KTX, I started again, this time making a sample database instead of air-coding. It worked much better. :-)
1) First, I changed the combobox names. FilterNAME looks like a form property and orderby is a form property. I changed their names to cboFilterName and cboOrderBy to make sure that they are recognizable as combos by humans reading the code and not mistaken by Access as the form property.
2) I changed the starting rowsource of salesmanfilterlist back to a standard Select statement, much like you had it to begin with: SELECT k.[ID], k.[Opportunity_Title], k.[Salesman], k.[open_date], k.[Distribution_co], k.[yearly_usage_KWh], k.[rating], k.[probability], k.[action_rqd] FROM keystoneopportunities AS k WHERE (((k.salesman)=[Forms]![OpportunityInput].[cboFilterNAME])); If you want to add an ORDER BY clause, you can do it, but don't make it dynamic.
I've posted the code behind the form below. The WHERE statement can handle the inclusion of a form reference, even to a null value -- no record are returned if cboFilterNAME is cleared. The ORDER BY clause is something that you need to either add entirely or remove entirely. Therefore, cboFilterName only contains the requery, whereas cboOrderBy rewrites the SQL entirely, depending on whether there is anything in cboOrderBy.
If you wanted to get fancier, your cboOrderBy could contain value pairs like:
"highest probability";"probability DESC"; "lowest probability";"probability ASC"; etc.
HTH,
pT72
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
|
Option Compare Database
Option Explicit
Private Sub cboFilterName_AfterUpdate()
Me!salesmanfilterlist.Requery
End Sub
Private Sub cboOrderBy_AfterUpdate()
If IsNull(cboOrderBy) Then
'There is no field to sort by, so set the rowsource back to the original
Me!salesmanfilterlist.RowSource = "SELECT k.[ID], k.[Opportunity_Title], k.[Salesman], k.[open_date], k.[Distribution_co], k.[yearly_usage_KWh], k.[rating], k.[probability], k.[action_rqd] FROM keystoneopportunities AS k WHERE (((k.salesman)=[Forms]![OpportunityInput].[cboFilterNAME]));"
Else
'there is a field specified - rewrite the rowsource to include ORDER BY clause
Me!salesmanfilterlist.RowSource = "SELECT k.[ID], k.[Opportunity_Title], k.[Salesman], k.[open_date], k.[Distribution_co], k.[yearly_usage_KWh], k.[rating], k.[probability], k.[action_rqd] FROM keystoneopportunities AS k WHERE (((k.salesman)=[Forms]![OpportunityInput].[cboFilterNAME])) ORDER BY " & Me!cboOrderBy & ";"
End If
Me!salesmanfilterlist.Requery
End Sub
|
|
|
|
|