Full Version: Report Orderby Not Working :(
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
exelprogrammer
So I've got 2 buttons that print out reports.. here's the code behind them:

CODE
Private Sub cmdReport_Click()
Dim rptSOP As String
Dim strFilter As String
strFilter = vbNullString
rptSOP = "SOPTraining"
strFilter = "[DocID]=" & cmbDocument.Value & " "


If chk1.Value <> "1" And chk1.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>1 "
End If
If chk2.Value <> "1" And chk2.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>2 "
End If
If chk3.Value <> "1" And chk3.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>3 "
End If
If chk4.Value <> "1" And chk4.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>4 "
End If
If chk5.Value <> "1" And chk5.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>5 "
End If
If chk6.Value <> "1" And chk6.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>6 "
End If
If chk7.Value <> "1" And chk7.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>7 "
End If
If chk8.Value <> "1" And chk8.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>8 "
End If
If chk9.Value <> "1" And chk9.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>36 "
End If
If chk0.Value <> "1" And chk0.Value <> "-1" Then
     strFilter = strFilter & " AND [TeamID]<>37 "
End If

If Me.chkInclude.Value = "1" Or Me.chkInclude.Value = "-1" Then
     DoCmd.OpenReport "SOPTraining2", acViewNormal, , strFilter, acHidden
     Report_SOPTraining2.OrderBy = "[Last Name]  "
     Report_SOPTraining2.OrderByOn = True
Else
     DoCmd.OpenReport rptSOP, acViewNormal, , strFilter, acHidden
     Report_SOPTraining.OrderBy = "[Last Name] "
     Report_SOPTraining.OrderByOn = True
End If




End Sub

Private Sub cmdTemplate_Click()
Dim rptTemplate As String
Dim strFilterTemplate As String
strFilterTemplate = vbNullString

rptTemplate = "SOPTraining-blank"
strFilterTemplate = "[DocID]=" & cmbDocument.Value


If chk1.Value <> "1" And chk1.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>1 "
End If
If chk2.Value <> "1" And chk2.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>2 "
End If
If chk3.Value <> "1" And chk3.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>3 "
End If
If chk4.Value <> "1" And chk4.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>4 "
End If
If chk5.Value <> "1" And chk5.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>5 "
End If
If chk6.Value <> "1" And chk6.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>6 "
End If
If chk7.Value <> "1" And chk7.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>7 "
End If
If chk8.Value <> "1" And chk8.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>8 "
End If
If chk9.Value <> "1" And chk9.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>36 "
End If
If chk0.Value <> "1" And chk0.Value <> "-1" Then
     strFilterTemplate = strFilterTemplate & " AND [TeamID]<>37 "
End If

If Me.chkInclude.Value = "1" Or Me.chkInclude.Value = "-1" Then
     DoCmd.OpenReport "SOPTraining-blank2", acViewNormal, , strFilterTemplate, acHidden
     [Report_SOPTraining-blank2].OrderBy = "[Last Name] "
     [Report_SOPTraining-blank2].OrderByOn = True
Else
     DoCmd.OpenReport rptTemplate, acViewNormal, , strFilterTemplate, acHidden
     [Report_SOPTraining-blank].OrderBy = "[Last Name] "
     [Report_SOPTraining-blank].OrderByOn = True
End If



And my queries are all a slight variation of this:
CODE
SELECT DISTINCT [Last Name]+", "+[First Name] AS Expr1, Employees.TeamID, TrainEmpDoc.EmpID, TrainEmpDoc.DocID, TrainEmpDoc.Date AS DocDate, Documents.Title, Documents.[Effective Date], Lookup.Value, Employees.Active, Employees.[Last Name]
FROM (TrainEmpAct INNER JOIN (((TrainEmpDoc INNER JOIN Documents ON TrainEmpDoc.DocID = Documents.ID) INNER JOIN Employees ON TrainEmpDoc.EmpID = Employees.ID) INNER JOIN Lookup ON Employees.TeamID = Lookup.ID) ON TrainEmpAct.EmpID = Employees.ID) INNER JOIN ActDocs ON (TrainEmpDoc.DocID = ActDocs.DocID) AND (TrainEmpAct.ActID = ActDocs.ActID) AND (Documents.ID = ActDocs.DocID)
WHERE (((Employees.Active)=1) AND ((TrainEmpAct.Date) Is Not Null) AND ((TrainEmpAct.ActID) Is Not Null))
ORDER BY Employees.[Last Name];

(I have tried this with the order by in the query before hand and without)

The weird thing is that it does infact order the results of the report.. the problem is that it always does it in descending order when I need it in ascending order. Even If I specify ASC on the OrderBy in the code, it still has no effect. I have tried applying the OrderBy to both Expr1 and just Last Name - still the same results. I will also post up a picture of my query incase that helps out a bit.

Wondering if maybe anyone has any idea as to why it could be acting strangely like this.

Also, I know the code is probably messy.. this was one of the first things I had to do when I started getting back into VBA. Plan on going back and eventually reworking all this and cleaning it up.
LPurvis
Hi

You have no Sorting and Grouping specified in your report?
That will always instantly negate (i.e. over-ride) any OrderBy clause you provide.
If you want simple, external ordering, then you'll have to remove that - or specify it (i.e. use it to supply the ordering).

On your code - you pass a filter as part of the open report request.
You then, once the report is open, request a new Ordering. This will effectivey rerun the report (each section being reprinted anew).
If you're using Access 2002 or newer - consider passing the OrderBy in as an OpenArgs and then setting the reports ordering itself internally as it opens.

Cheers.
exelprogrammer
Ah - sorting and grouping. Thats exactly what it was -- it had been so long since I made this report I completely forgot about that. After simply specifying Last Name in the sorting/grouping set to Ascending, it worked perfectly. Thanks for the suggestion, you saved me some stress smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.