• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Using loops to print records from Command Button

I need to allow a user to select a date range and print records from a table by pressing a command button.

I am having a problem with the code.  Here is a sample of the direction I am going.  I am just trying to get the logic to work, so I don't have the print coding in yet.  I am struggling with how to print a range of records based on criteria provided by the user.  
_________________

Private Sub cmdPrintReportPages_Click()

Dim inFieldjobPMP As Integer
Dim dtDateDataCollected As Date
Dim db As Database
Dim rstAssetCondition As Recordset

inFieldjobPMP = DLookup("FieldjobPMP", "tblAssetCondition")
dtDateDataCollected = DLookup("DateDataCollected", "tblAssetCondition")

DoCmd.RunCommand acCmdSaveRecord
Set db = CurrentDb()
Set rstAssetCondition = db.OpenRecordset("tblAssetCondition")


If Me!PrintFieldJobPMP = 1 Then
   
        Do Until rstAssetCondition.EOF
   
If inFieldjobPMP = 1 And dtDateDataCollected >= Me!PrintPagesBeginDate _  And dtDateDataCollected <= Me!PrintPagesEndDate Then

            Do Until rstAssetCondition.EOF

            DoCmd.Beep ' just using Beep to check logic
           
            rstAssetCondition.MoveNext
            Else
         'Exit Do  
            End If
           
        Exit Do
       
    Loop
   
   
Else

End If

End Sub


Are there any standard conventions for accomplishing this goal?

I am really struggling with using a loop or what.  I can seem to get DO Until or Do While to work.  Any code with working loops would be greatly appreciated.

Cliff
0
NothingNew
Asked:
NothingNew
1 Solution
 
brewdogCommented:
Where are you printing these records to? I can think of easier ways to do this, whether you're printing them one at a time or all together on a report, etc.

If you really want to do a loop, printing records one at a time, try something like this (I'm calling my recordset rs for brevity):

rs.MoveFirst
Do Until rs.eof
   If rs("FieldJobPMP") = 1 and rs("DateDataCollected") >= PrintPagesBeginDate and rs("DateDataCollected") <= PrintPagesEndDate Then
      . . . do your printing here
   End If
   rs.MoveNext
Loop

You might be better off, though, creating your recordset first, though, and filtering the records before creating the recordset, instead of checking every record one at a time. Something like this:

Dim strSQl as string
strSQL = "Select * from tblAssetCondition where FieldJobPMP = 1 and DateDataCollected Between #" & PrintPagesDateBegin & "# and #" & PrintPagesDateEnd & "#;"

set rs = currentdb().openrecordset(strSQL)
rs.movefirst   'this causes error 3201 if there are no records
do until rs.eof
  . . . do your print here
  rs.movenext
Loop

That's both simpler to code and faster than having to check every record after pulling the recordset. Hope this helps.

brewdog
0
 
bclark100898Commented:
How about the OpenReport method?

For example,

Private Sub Command16_Click()
    DoCmd.OpenReport "Inspection Report by Week", acViewPreview, , "[Date] >= #" & [FormStartDate] & "# And [Date] <= #" & [FormFinishDate] & "#"

End Sub

This code is part of a command button.  FormStartDate and FormEndDate are fields on the form that the user fills in.
0
 
NothingNewAuthor Commented:
I am using the latter of your suggestions.  I get a Too Few Parameters error on the Set Recordset command.

Please take a look at my code(really your code) and see if you find any errors.  It appears to be getting the stSQL correct.  I can look in debug and see that it evaluates it correctly.

I says that rstAssetCondition = Nothing when I place my cursor over the text in Debug mode.
_________________________________________
Private Sub cmdPrintReportPages_Click()

Dim inFieldjobPMP As Integer
Dim dtPrintPagesBeginDate As Date
Dim dtPrintPagesEndDate As Date
Dim dtDateDataCollected As Date
Dim db As Database
Dim rstAssetCondition As Recordset
Dim stSQL As String


inFieldjobPMP = DLookup("FieldjobPMP", "tblAssetCondition")
dtDateDataCollected = DLookup("DateDataCollected", "tblAssetCondition")
dtPrintPagesBeginDate = DLookup("PrintPagesBeginDate", "tblMainReport")
dtPrintPagesEndDate = DLookup("PrintPagesEndDate", "tblMainReport")



DoCmd.RunCommand acCmdSaveRecord
Set db = CurrentDb()


If Me!PrintFieldJobPMP = 1 Then
   
   stSQL = "Select * from qryConditionReport where FieldjobPMP = 1 and DateDataCollected Between #" & dtPrintPagesBeginDate & "# and #" & dtPrintPagesEndDate & "#;"
   
   Set rstAssetCondition = db.OpenRecordset(stSQL)<<Problem here
   rstAssetCondition.MoveFirst 'this causes error 3201 if no records
   Do Until rstAssetCondition.EOF
   DoCmd.Beep
   rstAssetCondition.MoveNext
   Loop
   
   
Else

End If
   
   
 
0
 
surrealismCommented:
1. Make sure that you can open qryConditionReport properly. (Just click Open button from Database window)
2. Make sure that you take care dtPrintPageBeginDate and dtPrintPageEndDate in case of value is null.
0
 
MoondancerCommented:
GREETINGS!

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.  Also in the Community Support link is a question on how experts can help, if they wish, on the cleaning of old and abandoned questions.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now