PC Review


Reply
Thread Tools Rate Thread

Can't get the code to sort the data properly.

 
 
DawnTreader
Guest
Posts: n/a
 
      25th Aug 2010
Here is the data in table utblPrintReportIDs

PrintReportID ReportID
4 36519
5 36522
6 36528
7 36529
8 36532
9 36537

i have a form that looks at the above table and then prints all the
reports according to the ID of the report in the table. i use a button
to execute the following code:

Dim rptName As String
Dim cn As adodb.Connection
Dim rptID As adodb.Recordset
Dim sqlrptID As String
Dim sqlrptIDdel As String
Dim strLinkCriteria As String
Dim rptIDField As String
Dim cntrptIDs As Integer

If IsNull(Me.cboReportType) Then
MsgBox "no report selected"
Me.cboReportType.SetFocus
Exit Sub
End If

Set cn = CurrentProject.Connection

sqlrptID = "SELECT ReportID FROM utblPrintReportIDs ORDER BY
ReportID ASC"
rptName = Me.cboReportType.Column(2)
rptIDField = Me.cboReportType.Column(3)

Set rptID = New adodb.Recordset
With rptID
Set .ActiveConnection = cn
.Source = sqlrptID
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.CursorLocation = adUseClient
.Open
.Sort = "ReportID ASC"
End With

Application.SetOption "Confirm Action Queries", 0
cntrptIDs = 0

rptID.MoveFirst
Do Until rptID.EOF
cntrptIDs = cntrptIDs + 1
MsgBox rptID.Fields("ReportID")
strLinkCriteria = strLinkCriteria & " OR " & rptIDField & " =
" & rptID.Fields("ReportID") '& " Or " & strLinkCriteria
Me.sfrmutblPrintReportIDs.Requery
rptID.MoveNext
Loop

strLinkCriteria = Right(strLinkCriteria, Len(strLinkCriteria) - 4)
DoCmd.OpenReport rptName, acViewNormal, , strLinkCriteria,
acNormal, "Print"

'kill everything
rptID.Close
cn.Close
Set cn = Nothing
Set rptID = Nothing

End Sub

i have been fighting it to actually print the reports in ascending
order of the ReportID. is there anything in this code that would be
causing it to come out randomly?
 
Reply With Quote
 
 
 
 
David W. Fenton
Guest
Posts: n/a
 
      26th Aug 2010
DawnTreader <(E-Mail Removed)> wrote in
news:f1274a70-2bf2-407f-aad7-(E-Mail Removed)
m:

> i have been fighting it to actually print the reports in ascending
> order of the ReportID. is there anything in this code that would
> be causing it to come out randomly?


I'd think your code would only print the last report in the list,
since the DoCmd.OpenReport is called after the Loop that walks
through the recordset.

Also, I wonder if this line:

Me.sfrmutblPrintReportIDs.Requery

....is interacting somehow. I don't see that you're using it for
anything anywhere and don't see any reason why you should want to
requery it for every row of your recordset.

--
David W. Fenton http://www.dfenton.com/
contact via website only http://www.dfenton.com/DFA/
 
Reply With Quote
 
DawnTreader
Guest
Posts: n/a
 
      31st Aug 2010
On Aug 26, 2:36*pm, "David W. Fenton" <NoEm...@SeeSignature.invalid>
wrote:
> DawnTreader <alanrt...@gmail.com> wrote innews:f1274a70-2bf2-407f-aad7-(E-Mail Removed)
> m:
>
> > i have been fighting it to actually print the reports in ascending
> > order of the ReportID. is there anything in this code that would
> > be causing it to come out randomly?

>
> I'd think your code would only print the last report in the list,
> since the DoCmd.OpenReport is called after the Loop that walks
> through the recordset.
>
> Also, I wonder if this line:
>
> * Me.sfrmutblPrintReportIDs.Requery
>
> ...is interacting somehow. I don't see that you're using it for
> anything anywhere and don't see any reason why you should want to
> requery it for every row of your recordset.
>
> --
> David W. Fenton * * * * * * * * *http://www.dfenton.com/
> contact via website only * *http://www.dfenton.com/DFA/


Hello and thanks for the reply

actually the loop is creating a criteria that i use when i open the
report.

Do Until rptID.EOF
cntrptIDs = cntrptIDs + 1
MsgBox rptID.Fields("ReportID")

strLinkCriteria = strLinkCriteria & " OR " & rptIDField & " =
" & rptID.Fields("ReportID")

notice how the strLinkCriteria is adding to itself.

Me.sfrmutblPrintReportIDs.Requery
rptID.MoveNext
Loop

i actually figured it out, here is the corrected code:


Dim rptName As String
Dim cn As adodb.Connection
Dim rptID As adodb.Recordset
Dim sqlrptID As String
Dim strLinkCriteria As String
Dim rptIDField As String
Dim cntrptIDs As Integer

If IsNull(Me.cboReportType) Then
MsgBox "no report selected"
Me.cboReportType.SetFocus
Exit Sub
End If

Set cn = CurrentProject.Connection

sqlrptID = "SELECT ReportID FROM utblPrintReportIDs ORDER BY
ReportID DESC"
rptName = Me.cboReportType.Column(2)
rptIDField = Me.cboReportType.Column(3)

Set rptID = New adodb.Recordset
With rptID
Set .ActiveConnection = cn
.Source = sqlrptID
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With

Application.SetOption "Confirm Action Queries", 0
cntrptIDs = 0

rptID.MoveFirst
Do Until rptID.EOF
cntrptIDs = cntrptIDs + 1
strLinkCriteria = strLinkCriteria & " OR " & rptIDField & " =
" & rptID.Fields("ReportID")
rptID.MoveNext
Loop

strLinkCriteria = Right(strLinkCriteria, Len(strLinkCriteria) - 4)
DoCmd.OpenReport rptName, acViewNormal, , strLinkCriteria,
acNormal, "Print"

'kill everything
rptID.Close
cn.Close
Set cn = Nothing
Set rptID = Nothing

End Sub

the requery you were wondering about is a form and yeah it was legacy
code. i used to delete the list as i printed it, now i dont want to do
that so it is gone.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sort function for dates does not sort properly in Office 2007 Exc. Rosalie Microsoft Excel Worksheet Functions 1 22nd Nov 2007 10:25 PM
Excel 2003 Data Sort & Subtotaling not working properly =?Utf-8?B?Um9pYm4gTCBUYXlsb3I=?= Microsoft Excel Misc 5 8th Dec 2005 05:59 PM
Can't sort properly Ken Jeffery Microsoft Excel Misc 1 15th Jul 2004 01:38 AM
vba code to sort data? eadie Microsoft Excel Programming 3 23rd Dec 2003 02:09 PM
Graph Data Sheet Won't Sort Properly yussi1870 Microsoft Access Forms 0 24th Jul 2003 10:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:57 PM.