Solved

Using loops to print records from Command Button

Posted on 1998-12-04
5
282 Views
Last Modified: 2008-03-17
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
Comment
Question by:NothingNew
5 Comments
 
LVL 10

Accepted Solution

by:
brewdog earned 20 total points
ID: 1970081
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
 
LVL 2

Expert Comment

by:bclark100898
ID: 1970082
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
 

Author Comment

by:NothingNew
ID: 1970083
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
 
LVL 1

Expert Comment

by:surrealism
ID: 1970084
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6875837
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now