Okay, just tried it, and I am getting the same result as you.
Access silently rejects the OrderBy setting. If you type the value into the
property in design view, the property remains until you set OrderByOn to
True. Then the property is lost.
It looks like Access can't handle the property if it contains the function
name. I have no problem with that, but I do have a problem with it failing
silently, where it does not notify you that the property setting failed. In
my view, that constitutes a bug.
Here's a hint as to what goes wrong. If you try:
Forms(0).OrderBy = "Rnd()"
Forms(0).OrderByOn = True
and then ask:
? Forms(0).OrderBy
you get the answer:
[Rnd()]
The square brackets indicate that Access has turned the function call into a
parameter. The problem is therefore with the interpretation of the value
assigned to the property.
As a workaround, you could reassign the RecordSource of the form. For
example, if your form is bound to:
SELECT Client.* FROM Client;
you could use:
Me.RecordSource = "SELECT Client.* FROM Client ORDER BY
Right(Client.Surname,2);"
(Of course, all of this is highly inefficient, and I suspect that any design
that needs to do this should probably have the last 2 characters of the
field as a separate field so that the data is atomic.)
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
ctdak said:
Allen,
That didn't work either. Same result.
ctdak
Allen Browne said:
Try swapping the 2 lines, so you set OrderByOn after OrderBy.
I have a builder query which is sorting on the last two characters of a
text
field, "Right([Fieldname],2)". This has always worked fine in the
query.
However, I am trying to replace this query with code in a Form_Open
event,
as
follows:
OrderByOn = True
OrderBy = "Right([Fieldname],2)"
This does not work, presumeably because the form's OrderBy property
will
only take field names and not expressions including field names. So,
how
can
I code a replacement for this builder query?