Sort Button question for Allen Browne

S

steve

I found this code in another post and am trying to use it on my form. When
I run the code I get the following error: "Microsoft Access cant find the
field '|' referred to in your expression."

I have searched the function several times and can't spot the error.

Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
On Error GoTo Err_SortForm 'Provided by Allen Browne
'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")
Dim sForm As String ' Form name (for error handler).

sForm = frm.Name
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

Exit_SortForm:
Exit Function


Err_SortForm: ' Label to jump to on error.
MsgBox Err.Number & Err.Description ' Place error handling here.

'Call LogError(Err.Number, Err.Description, conMod & ".SortForm()",
"Form
'= " & sForm & "; OrderBy = " & sOrderBy)
Resume Exit_SortForm
End Function

TIA

Steve
 
S

steve

This is the code for the button, I forgot to include in the first post.

Private Sub SortRecordsButton_Click()
On Error GoTo Err_SortRecordsButton_Click

If Me.Dirty Then Me.Dirty = False

Call SortForm([frmT-A], "[IncidentDate]")



Exit_SortRecordsButton_Click:
Exit Sub

Err_SortRecordsButton_Click:
MsgBox Err.Description
Resume Exit_SortRecordsButton_Click

End Sub

Again TIA for all the help.

Steve
 
A

Allen Browne

You need to pass a reference to the form as the first argument.

Try:
Call SortForm(Me, "[IncidentDate]")

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

steve said:
This is the code for the button, I forgot to include in the first post.

Private Sub SortRecordsButton_Click()
On Error GoTo Err_SortRecordsButton_Click

If Me.Dirty Then Me.Dirty = False

Call SortForm([frmT-A], "[IncidentDate]")



Exit_SortRecordsButton_Click:
Exit Sub

Err_SortRecordsButton_Click:
MsgBox Err.Description
Resume Exit_SortRecordsButton_Click

End Sub

Again TIA for all the help.

Steve
steve said:
I found this code in another post and am trying to use it on my form.
When I run the code I get the following error: "Microsoft Access cant find
the field '|' referred to in your expression."

I have searched the function several times and can't spot the error.

Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
On Error GoTo Err_SortForm 'Provided by Allen Browne
'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")
Dim sForm As String ' Form name (for error handler).

sForm = frm.Name
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

Exit_SortForm:
Exit Function


Err_SortForm: ' Label to jump to on error.
MsgBox Err.Number & Err.Description ' Place error handling here.

'Call LogError(Err.Number, Err.Description, conMod & ".SortForm()",
"Form
'= " & sForm & "; OrderBy = " & sOrderBy)
Resume Exit_SortForm
End Function
 
S

steve

Thanks Allen, I had Call SortForm([frmT-A] ,"[IncidentDate]") worked
perfectly when I changed frmT-A to Me.

And once again Thanks for all the help!
Steve
Allen Browne said:
You need to pass a reference to the form as the first argument.

Try:
Call SortForm(Me, "[IncidentDate]")

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

steve said:
This is the code for the button, I forgot to include in the first post.

Private Sub SortRecordsButton_Click()
On Error GoTo Err_SortRecordsButton_Click

If Me.Dirty Then Me.Dirty = False

Call SortForm([frmT-A], "[IncidentDate]")



Exit_SortRecordsButton_Click:
Exit Sub

Err_SortRecordsButton_Click:
MsgBox Err.Description
Resume Exit_SortRecordsButton_Click

End Sub

Again TIA for all the help.

Steve
steve said:
I found this code in another post and am trying to use it on my form.
When I run the code I get the following error: "Microsoft Access cant
find the field '|' referred to in your expression."

I have searched the function several times and can't spot the error.

Function SortForm(frm As Form, ByVal sOrderBy As String) As Boolean
On Error GoTo Err_SortForm 'Provided by Allen Browne
'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")
Dim sForm As String ' Form name (for error handler).

sForm = frm.Name
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

Exit_SortForm:
Exit Function


Err_SortForm: ' Label to jump to on error.
MsgBox Err.Number & Err.Description ' Place error handling here.

'Call LogError(Err.Number, Err.Description, conMod & ".SortForm()",
"Form
'= " & sForm & "; OrderBy = " & sOrderBy)
Resume Exit_SortForm
End Function
 

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