Silvio said:
Thanks Dirk, this works just as I wanted it! Taking this a step further,
is
there a way to write the code once for each filed (Public constant) and
use
it over and over in my project throughout my forms? If so, would you be so
kind to guide me step-by-step on how to do so?
I'm not completely sure I understand what you want, but it wouldn't be hard
to turn the code into a function that could be called from any form. One
would want to pass a reference to the form and the name of the field. Try
pasting this function into a standard module:
'----- start of code -----
Function fncOrderByField( _
frm As Access.Form, _
strFieldName As String, _
Optional blnDesc As Boolean)
' Order the form <frm> by the field whose name is
' passed as <strFieldName>. If the optional argument
' <blnDesc> is specified, then the requested sequence
' is Descending; otherwise it is Ascending. However,
' if the form is already ordered by the specified field
' in the specified sequence (ascending/decending), then
' reverse the sequence on that field.
Dim strSeqCurrent As String
Dim strSeqWanted As String
strSeqWanted = strFieldName & IIf(blnDesc, " DESC", "")
With frm
' Determine current sequence, standardized for
' comparison to strSeqWanted.
If .OrderByOn Then
strSeqCurrent = .OrderBy
If strSeqCurrent Like "* ASC" Then
strSeqCurrent = Left$(strSeqCurrent, Len(strSeqCurrent) - 4)
End If
End If
If strSeqCurrent <> strSeqWanted Then
.OrderBy = strSeqWanted
Else
' We're currently sorted as originally requested,
' so flip the sort sequence on that field.
If blnDesc Then
' Currently sorted DESC; change to ASC.
.OrderBy = strFieldName
Else
.OrderBy = strFieldName & " DESC"
End If
End If
.OrderByOn = True
End With
End Function
'----- end of code -----
Then, on any form where you want to use this feature, you can put a direct
call to the function into the OnClick property of each label, like this:
=fncOrderByField([Form],"EmployeeName",True)
The above would cause the form to be sorted by EmployeeName descending,
unless the form is already sorted that way, in which case it would reverse
the sort order to EmployeeName ascending. Note that the keyword [Form] is
exactly what should be passed by the function expression; I do *not* intend
that you should replace that with the name of the form.
The way the function is written, you can leave off the third argument
(specified above as True) if you want the form to be sorted first as
ascending, rather than descending.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)