Sort Continuous from combo column(2) (1 Viewer)

Danick

Registered User.
Local time
Today, 06:15
Joined
Sep 23, 2008
Messages
351
Trying to find a way to sort a continuous form from a column in one of the combo boxes. I can add an unbound field in the continuous form and call it TeamSortOrder and make the control source the second column of one of the combo boxes. For example, =[TeamFunction].[Column](2)
This unbound [TeamSortOrder] field does show the second column from that combo box, but I can't find any way to sort the continuous form by this field. This VBA isn't working.

Me.OrderBy = "TeamSortOrder ASC"

Any suggestions?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:15
Joined
Oct 29, 2018
Messages
21,467
Hi. Just to be clear, does the second column of your combobox contain field names for your data source?
 

Danick

Registered User.
Local time
Today, 06:15
Joined
Sep 23, 2008
Messages
351
No. Just the first column does.
I tried to create a query and add both to the datasource, but then I couldn't edit the form anymore.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:15
Joined
Oct 29, 2018
Messages
21,467
No. Just the first column does.
I tried to create a query and add both to the datasource, but then I couldn't edit the form anymore.
Okay, if the column does not contain field names, what were you expecting to use for sorting? For example, if you had a table like so,

SELECT FirstName, LastName FROM Students

Then you can sort it like this, right?

SELECT FirstName, LastName FROM Students ORDER BY LastName

You wouldn't sort it like this though.

SELECT FirstName, LastName FROM Students ORDER BY 'Jones'

Make sense? Is this what you're trying to do?
 

Danick

Registered User.
Local time
Today, 06:15
Joined
Sep 23, 2008
Messages
351
I've got a table called CompanyTeams and one of the fields is called TeamFunction. I'd like to sort by TeamFunction, but not by actual name in that field. So I created another table called TeamSortOrder that has the TeamFunction as well as a numerical field called TeamSortOrder. So basically, I'd like to show the CompanyTeams in the Continuous Form but sorted by the TeamSortOrder table.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:15
Joined
Oct 29, 2018
Messages
21,467
I've got a table called CompanyTeams and one of the fields is called TeamFunction. I'd like to sort by TeamFunction, but not by actual name in that field. So I created another table called TeamSortOrder that has the TeamFunction as well as a numerical field called TeamSortOrder. So basically, I'd like to show the CompanyTeams in the Continuous Form but sorted by the TeamSortOrder table.
Hi. Thanks for the additional information. So, let's try a different example.

SELECT Rank, LastName FROM Soldiers

Now, let's say a Lieutenant is superior to an Ensign or a Corporal, for example, that means we can't sort the Rank column alphabetically. Using your example, we may create a table with a sorting order for ranks. In that case, the above query might now look like this:

SELECT Soldiers.Rank, Soldiers.LastName FROM Soldiers
INNER JOIN RankSortTable
ON Soldiers.Rank=RankSortTable.Rank
ORDER BY RankSortTable.RankSort

To apply the above to your form, your combo column must contain the name of the field where you stored the sorting order for your team functions.

Hope that helps...
 

Danick

Registered User.
Local time
Today, 06:15
Joined
Sep 23, 2008
Messages
351
Well that works very well for the sorting - does exactly what I'm looking to do. But now I can't add a record or make any changes in the continuous form anymore. When ever I try to make a change, I get a tone. So looks like I'm going to have to go back so I can gain use of the form again.
Thanks.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:15
Joined
May 21, 2018
Messages
8,527
Make sure in table TeamSortOrder that TeamFunction is a primary key. If not your query become non-editable. The tone is because you are probably getting a message saying that the recordest is not editable.
 

strive4peace

AWF VIP
Local time
Today, 05:15
Joined
Apr 3, 2020
Messages
1,004
hi @Danick

according to my notes, you can sort a form by the column of a combo that's not in the RecordSource using this for Order By:

[Lookup_ControlName].[ColumnName]

isn't Access totally amazing?

EDIT 13 July corrected the reference

WHERE
  • ControlName is the Name property of the control and "Lookup_" is added to the beginning of the reference
  • ColumnName is the field name or calculated field name in the SQL

It works! for form.OrderBy
You can also add DESC to sort in descending order, and combine the sort with other fields in record source, and even other lookup columns. For instance:

Rich (BB code):
me.OrderBy =  "[Lookup_ContactID].[LastFirst], Ordr"
'or
me.OrderBy =  "[Lookup_CategoryID].[Category] DESC, [Lookup_ContactID].[LastFirst]"
'or
me.OrderBy = "[Lookup_CategoryID].[Category],[Lookup_ContactID].[FirstLast]"
and then:
Rich (BB code):
me.OrderByOn = True

These examples use different columns of the same combobox named ContactID. Here is what it looks like:
1657733922534.png


And what isn't showing is the Bound Column, ContactID

Here's the SQL:

Code:
SELECT c_Contact.ContactID
, ([MainName]+", ") & [NameA] AS LastFirst
, ([NameA]+" ") & [MainName]  AS FirstLast
FROM c_Contact
ORDER BY c_Contact.MainName,  c_Contact.NameA;

In this example:
MainName is the Last name of a human or the company name of a company
NameA is the first name of a human (or, possibly if desired, the division name of a company)

the form itself is based on a cross-reference table between contacts and category with Long Integer foreign keys. There is an Ordr column when a contact is in more than one category, for order of importance

1657734813200.png


Here is the design view of the form:

1657734975884.png


Each of the orange command buttons in the form header run VBA to change the form's OrderBy clause and set OrderByOn to True. The last column is a textbox with an expression that displays a column from the ContactID combo box -- but still the form can sort by it!
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:15
Joined
May 7, 2009
Messages
19,230
But now I can't add a record or make any changes in the continuous form
you can use Left Join instead of Inner Join.
your table on the Left side while TeamOrder table on the right.
 

Danick

Registered User.
Local time
Today, 06:15
Joined
Sep 23, 2008
Messages
351
Thanks for all the help.
I had originally created the table with an autonumber ID primary key. Deleting that field and making the TeamFunction the primary key fixed the non-edible issue. Now the form sorts perfectly and is editable.
 

LarryE

Active member
Local time
Today, 03:15
Joined
Aug 18, 2021
Messages
584
Trying to find a way to sort a continuous form from a column in one of the combo boxes. I can add an unbound field in the continuous form and call it TeamSortOrder and make the control source the second column of one of the combo boxes. For example, =[TeamFunction].[Column](2)
This unbound [TeamSortOrder] field does show the second column from that combo box, but I can't find any way to sort the continuous form by this field. This VBA isn't working.

Me.OrderBy = "TeamSortOrder ASC"

Any suggestions?
Can you go to the control and then sort the field like this:
DoCmd.GoToControl "ControlNameHere"
DoCmd.RunCommand acCmdSortAscending
 

strive4peace

AWF VIP
Local time
Today, 05:15
Joined
Apr 3, 2020
Messages
1,004
I just tested @LarryE's method and RunCommand acCmdSortAscending sorts a form by the first visible column of the combo or listbox. Therefore, @Danick, there is no need not to use the AutoNumber primary key and Long Integer foreign key.

If you want to sort by multiple fields, do them in reverse order

EDIT ... but there is a better way -- see next post ;)
 
Last edited:

strive4peace

AWF VIP
Local time
Today, 05:15
Joined
Apr 3, 2020
Messages
1,004
@Danick -- please note:

I modified my previous post about being able to sort a form by the column of a combobox ... my notes were almost right -- you CAN do it!

Now Post #9
shows you how to sort a form by any column of a combobox, not just the first visible column as the macro method with RunCommand does -- and you can add other fields to the sort.

Please do not structure the data improperly because you want the interface to sort a form by the column of a combo and you didn't know how to do that.

Access always amazes me!
 
Last edited:

Users who are viewing this thread

Top Bottom