By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,478 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,478 IT Pros & Developers. It's quick & easy.

Changing the OrderBy of a subform programatically

P: 4
I have a Form with a (Continuous) Subform that lists the results of a query. There's a header within the Subform that has several elements you can click on to programatticaly change the OrderBy of the Subform then refresh it. An example of the code I use is here:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CompanySort_Click()
  2. If (Me.OrderBy = "Company") Then
  3. Me.OrderBy = "Company DESC"
  4. Else
  5. Me.OrderBy = "Company"
  6. End If
  7. Me.Refresh
  8. End Sub
This works fine when the Form (which is named "Results Subform") is run by itself, but doesn't work when run as part of the parent Form ("Results"). The name of the Subform control is "Report subform", can't remember why it's different now...

What am I doing wrong? I'm sure it's simple, but I'm stumped, any help is much appreciated.
Nov 17 '11 #1

✓ answered by sierra7

Instead of Me.Refresh try;
Expand|Select|Wrap|Line Numbers
  1. Me.OrderByOn = True
Works for me!
S7

Share this Question
Share on Google+
6 Replies


TheSmileyCoder
Expert Mod 100+
P: 2,321
Im not really sure why that is happening.

First off, are you sure the code is running? Have you tried stepping through it?
Nov 17 '11 #2

P: 4
Thanks for your reply. Yes, code appears to be running - I've tried putting msgboxes after each significant bit of code to check, which still come up. It works fine when the "Results Subform" is open as a form in itself, but not when it's a subform. Could it be that "me" is referring to the parent rather than the subform?
Nov 17 '11 #3

Expert 100+
P: 446
Instead of Me.Refresh try;
Expand|Select|Wrap|Line Numbers
  1. Me.OrderByOn = True
Works for me!
S7
Nov 17 '11 #4

TheSmileyCoder
Expert Mod 100+
P: 2,321
I would try to start with testing Sierra7's suggestion.

To refer to the parent form you use Me.Parent.
Nov 17 '11 #5

P: 4
Brilliant, that works! Thanks to sierra7 and TheSmileyCoder!
Nov 17 '11 #6

NeoPa
Expert Mod 15k+
P: 31,709
Setting Me.OrderByOn = True certainly works when it wasn't previously set, but if you find it doesn't when changing from one sort order to another (In other words it works only for the first change - Help doesn't specify and I don't have time to set up a test myself) then a .Requery call should do it for you.
Nov 17 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.