Sort Assending When Opening A Form

oneilldm

Registered User.
Local time
Yesterday, 22:55
Joined
Dec 10, 2008
Messages
19
I'd like to have a form sort by my ID field automatically, every time the form is opened.

Is there an easy way to do this?

Thanks
 
Thank you for the reply.

I'm almost done with this project. I was hoping that I could just use a couple lines of code.
 
Thank you for the reply.

I'm almost done with this project. I was hoping that I could just use a couple lines of code.
You should just go modify the control source than to have to write code to do it. It is better to do that way, but if you must you can do it in the form's On Load event:

Me.OrderBy = Me!YourFieldName
Me.OrderByOn = True
 
Thanks for your help!
It's prompting me for a Parameter Value.....???
I haven't figured that one out yet.
 
Thanks for your help!
It's prompting me for a Parameter Value.....???
I haven't figured that one out yet.

That usually happens when you are trying to use something that doesn't exist. In other words, if you had a field that was named MyField and were trying to use this code:

Me!ThatField

Then it would give you a prompt for

[ThatField]

because ThatField doesn't exist. I meant to put MyField instead.

So verify that the code you are using is referring to an actual field in the recordsource of the form. AND make sure you used the form's LOAD event and not the OPEN event because the recordsource is not available in the OPEN event.
 
Thanks...
I'm using the Form_Load event.... and I think I have the field in there right. (It's just called ID)

If I type in me. I get a dropdown and I can select my field from the list.

The me!... I don't understand. I don't know what the exclamation point is for. :o
 
Make a little macro

GoToControl for the first action line and enter the field name at the bottom of the macro design screen. Don't enclose in brackets or commas

RunCommand for the second action line and from the bottom of the macro design screen select SortAscending

Place the macro on the Open event for the form. This does exactly the same as clicking in the field and then clicking A to Z on the toolbar.

I have not found code to be a good way to do this. It depends what else is involved with the form. I think it fouls up for me because I have code that scales the form for screen resolution.

DoCmd.GoToControl "FieldName"
DoCmd.RunCommand acCmdSortAscending
 
Oh...Hot Dog... I figured out what I was doing wrong!!! :)

Boy... the smallest thing wrong...

Me.OrderBy = "ID"
Me.OrderByOn = True


This worked like a dream!!! Thanks boblarson!!!

And thanks Mike375... I'm going to give that a try.
I like having options!!!
 
Personally I would stick with Bob's solution. The idea of having to move focus on the form in order to sort of a specific field seems a little messy when you can address the sort properties for the form directly as shown by Bob.

Also, if you are on the path of using VBA then I would recommend you avoid using macros. Otherwise you are using two methodologies side by side and you start to struggle with what is controlling what.

A couple of explanations of .(dot) !(bang)
here
and here

Google dot bang access for more

Chris
 

Users who are viewing this thread

Back
Top Bottom