Tech Support Guy banner
Status
Not open for further replies.

Solved: Sorting forms by field?

853 views 4 replies 3 participants last post by  smooth 
#1 ·
Hello again,

I have a button that when pressed, opens up a form that shows records that meet a certain criteria. Now, I know that I can just put my cursor where I want, and go Records --> Sort --> Sort Ascending while I'm looking at the form.

However, I want it to where it automatically sorts the records ascending by the last name field without having to use that method. As in, I want it to open up sorted by the last name. Is there anyway of accomplishing this?

Thanks.
 
#3 ·
Thanks for the reply, I already have it set up on the buttons to bring up the form with certain criteria met. Here is the code for one of the buttons:

Private Sub active_agents_Click()
On Error GoTo Err_active_agents_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "LatestAgents"
DoCmd.OpenForm stDocName
Forms!LatestAgents.RecordSource = "SELECT * FROM [Agents]" & "WHERE [Agents].[ActiveStatus] = -1;"
Exit_active_agents_Click:
Exit Sub

Err_active_agents_Click:
MsgBox Err.Description
Resume Exit_active_agents_Click

End Sub

Now, is there some line of code that I can add to this code to sort the form by the field "Last Name", like a Form!Sort or something like that?
 
#4 ·
Use the ORDER BY clause of SQL to sort your records, like so:

Code:
'Change this line of your code; I've broken it into multiple lines for
'clarity.
Forms!LatestAgents.RecordSource = "SELECT * " & _
                                  "FROM [Agents] " & _
                                  "WHERE [Agents].[ActiveStatus] = -1 " & _
                                  "ORDER BY [Agents].[LastName];"
HTH

chris.
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top