Unable to sort a calculated field in a datasheet form
Hi experts,
In ds form i have some calculated columns(either by functions, either they are clalculated as the sum of other fields) and i want to allow the user to sort ascendig or descending, but these buttons are disabled? Is there any work around or just because these colums are calculated when the form is open and their values are not saved anywhere i cannot make a sorting based on them?
You can't do this via the standard Access interface buttons, but you can add your own Sort buttons to the form and sort the recordsource by your calculated columns. For example:
Sub SortByCalcColumn()
Me.Recordsource = "SELECT blah blah ORDER BY NameOfMyCalcColumn"
End Sub
A work around might be to change the query in an append query and store the result in a temp table.
Trigger a delete * from tblTemp and the append query in the main form's OnOpen and by using the datasheet for the subform, your standard sort will work.
The sort buttons are only disabled with you create expressions directly in the form. For example, if you have, in txtTotal:
= Quantity * UnitPrice
That expression is evaluated by the Form engine, not by Jet, so that it cannot use it as sort criteria. However, if you include it in the query, it will work. Click on the builder button to create a query, include the (table name).* in order to have all fields and add this calculated field:
Total: Quantity * UnitPrice
Return to your form and replace the expression with 'Total'. This way, Jet know what the field contains and will happily sort by it.
As long as it's a simple calculation harfang's solution will work, however the calculated field can't be updated for obvious reasons. t will however reflect a change to the qty or price.
GroupBy queries with a Sum() or other function however won't allow changes.
You can always set the field properties to lock it for update and to be skipped when tabbing.
Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.
Sub SortByCalcColumn()
Me.Recordsource = "SELECT blah blah ORDER BY NameOfMyCalcColumn"
End Sub