Sorting subfomr on combobox

A

Adam Milligan

I am using Access 2007 on WIndows Vista. I have a subform with a single
combo box in it. The combo box's row source has two columns, the ID field
and then a concatenated text field that shows a person's name followed by
their company. What I woudl like to do is give the option to the user to
have the combo box display the person's company followed by their name. With
the following code, I have succeeded in chaging the combo box back and forth:

Private Sub cmdByContact_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst] & ' - ' & [txtReferralSourceOrganization] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] & ' - ' &
[txtReferralSourceOrganization];"

Me.cboReferralSource_FK.Requery

End Sub

Private Sub cmdByOrg_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceOrganization] & ' -
' & [txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceOrganization] & ' - ' & [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst];"

Me.cboReferralSource_FK.Requery

End Sub


That is all well and good, but what I need to do now is make the existing
records in the subform fall into alphabetical order by the new text displayed
in the combo box. I am certain it is possible because I can just right click
the combo box and select "Sort AZ" and it does what I want. And in my
experience so far, ANYHTHING you can do by right clicking, you can do with
code. I Just can't seem to find the magic Me.DoWhatTheHeck.IWant command.
Any help would be appreciated.

Thanks

Adam
 
B

Beetle

Try this;

Private Sub cmdByContact_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst] & ' - ' & [txtReferralSourceOrganization] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] & ' - ' &
[txtReferralSourceOrganization];"

Me.cboReferralSource_FK.Requery
Me.OrderBy = "txtReferralSourceLast"

End Sub

Private Sub cmdByOrg_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceOrganization] & ' -
' & [txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceOrganization] & ' - ' & [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst];"

Me.cboReferralSource_FK.Requery
Me.OrderBy = "txtReferralSourceOrganization"

End Sub

--
_________

Sean Bailey


Adam Milligan said:
I am using Access 2007 on WIndows Vista. I have a subform with a single
combo box in it. The combo box's row source has two columns, the ID field
and then a concatenated text field that shows a person's name followed by
their company. What I woudl like to do is give the option to the user to
have the combo box display the person's company followed by their name. With
the following code, I have succeeded in chaging the combo box back and forth:

Private Sub cmdByContact_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst] & ' - ' & [txtReferralSourceOrganization] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] & ' - ' &
[txtReferralSourceOrganization];"

Me.cboReferralSource_FK.Requery

End Sub

Private Sub cmdByOrg_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceOrganization] & ' -
' & [txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceOrganization] & ' - ' & [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst];"

Me.cboReferralSource_FK.Requery

End Sub


That is all well and good, but what I need to do now is make the existing
records in the subform fall into alphabetical order by the new text displayed
in the combo box. I am certain it is possible because I can just right click
the combo box and select "Sort AZ" and it does what I want. And in my
experience so far, ANYHTHING you can do by right clicking, you can do with
code. I Just can't seem to find the magic Me.DoWhatTheHeck.IWant command.
Any help would be appreciated.

Thanks

Adam
 
A

Adam Milligan

Beetle-

Thanks! Your code along with me.SortByOn=true worked like a charm!

Adam

Beetle said:
Try this;

Private Sub cmdByContact_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst] & ' - ' & [txtReferralSourceOrganization] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] & ' - ' &
[txtReferralSourceOrganization];"

Me.cboReferralSource_FK.Requery
Me.OrderBy = "txtReferralSourceLast"

End Sub

Private Sub cmdByOrg_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceOrganization] & ' -
' & [txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceOrganization] & ' - ' & [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst];"

Me.cboReferralSource_FK.Requery
Me.OrderBy = "txtReferralSourceOrganization"

End Sub

--
_________

Sean Bailey


Adam Milligan said:
I am using Access 2007 on WIndows Vista. I have a subform with a single
combo box in it. The combo box's row source has two columns, the ID field
and then a concatenated text field that shows a person's name followed by
their company. What I woudl like to do is give the option to the user to
have the combo box display the person's company followed by their name. With
the following code, I have succeeded in chaging the combo box back and forth:

Private Sub cmdByContact_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst] & ' - ' & [txtReferralSourceOrganization] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] & ' - ' &
[txtReferralSourceOrganization];"

Me.cboReferralSource_FK.Requery

End Sub

Private Sub cmdByOrg_Click()

Me.cboReferralSource_FK.RowSource = "SELECT
tblReferralSource.intReferralSourceID, [txtReferralSourceOrganization] & ' -
' & [txtReferralSourceLast] & ', ' & [txtReferralSourceFirst] AS
ReferralSource FROM tblReferralSourceOrganization INNER JOIN
tblReferralSource ON
tblReferralSourceOrganization.intReferralSourceOrganizationID =
tblReferralSource.intReferralSourceOrganization_FK ORDER BY
[txtReferralSourceOrganization] & ' - ' & [txtReferralSourceLast] & ', ' &
[txtReferralSourceFirst];"

Me.cboReferralSource_FK.Requery

End Sub


That is all well and good, but what I need to do now is make the existing
records in the subform fall into alphabetical order by the new text displayed
in the combo box. I am certain it is possible because I can just right click
the combo box and select "Sort AZ" and it does what I want. And in my
experience so far, ANYHTHING you can do by right clicking, you can do with
code. I Just can't seem to find the magic Me.DoWhatTheHeck.IWant command.
Any help would be appreciated.

Thanks

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top