Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort by more then one field in a form

Status
Not open for further replies.
I am trying to sort by more then one column. I am not having any problems sorting on one column and my code is working fine when it is like this:

Private Sub Planner_Label_Click()
Call SortForm(Me, "Planner")
End Sub

When I am trying to sort by more then one column such as:

Private Sub Planner_Label_Click()
Call SortForm(Me, "Planner", "Planb")
End Sub

I get a compile error in my form stating:
“wrong number of arguments or invalid property assignment”

How can I change my VBA code to sort by more then one field.

Thanks a bunch!
 
What is SortForm? I assume it is a user defined function, in which case if you have written it to have a single parameter passed in, it will error.

If you select the SortForm word, right click definition it should take you to the function.

If you want to limit the sort by statically (i.e. a max of 3 sort columns) then you could modify the function to accept one parameter plus n optional parameters using the optional keyword, & then modifying the function slightly as required.

Alternatively, for truly dynamic capabilities you will need to rethink the function & it use...

James Goodman MCSE, MCDBA
 
James,

I attached my function (that I inherited) if you can tell me what I need to change in here that would be great. THANK YOU FOR YOU HELP!!

Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
'Purpose: Set a form's OrderBy to the string. Reverse if already set.
'Return: True if success.
'Usage: Command button above a column in a continuous form:
' Call SortForm(Me, "MyField")

If Len(sOrderBy) > 0 Then
' Reverse the order if already sorted this way.
If frm.OrderByOn And (frm.OrderBy = sOrderBy) Then
sOrderBy = sOrderBy & " DESC"
End If
frm.OrderBy = sOrderBy
frm.OrderByOn = True
' Succeeded.
SortForm = True
End If
End Function
 
Ahh, in that case I think you might be able to do this without making any changes.

At the moment you call the function like:
Call SortForm(Me, "Planner")
For a single orderby,

And like:
Call SortForm(Me, "Planner", "Planb")
for multiple orderby's which obviously errors.

Looking at the help file, the orderby property is a read/write string, which means you should be able to submit a single text string, containing one or more entries to apply the sort.

So, try the following:
Call SortForm(Me, "Planner, Planb")

I hope it works. :)



James Goodman MCSE, MCDBA
 
Thanks James I will try that tonight!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top