Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

How cycle through reports and print them

I have a report that has a query as it's record source.  The SQL of the query looks like:

SELECT tblClientSignAudit.ClientName, tblClientSignAudit.ClientAddress, tblClientSignAuditDetail.Location, tblClientSignAuditDetail.SiteImageFolder, tblTypeLU.Code, [SizeWidth] & "x" & [SizeHeight] & "x" & [SizeDepth] AS [Size], tblCompositeLU.Description AS Composition, tblConditionLU.Description AS Condition, tblClientSignAuditDetail.Electric, tblClientSignAuditDetail.Message, tblClientSignAuditDetail.Qty, tblClientSignAuditDetail.ClientAuditID, tblClientSignAuditDetail.TagNumber, tblTypeLU.Description, tblAuditTypesLU.Description, tblClientSignAudit.LogoFile
FROM (tblClientSignAudit LEFT JOIN tblAuditTypesLU ON tblClientSignAudit.SheetDescription = tblAuditTypesLU.ID) INNER JOIN (((tblClientSignAuditDetail LEFT JOIN tblTypeLU ON tblClientSignAuditDetail.Type = tblTypeLU.ID) LEFT JOIN tblCompositeLU ON tblClientSignAuditDetail.Composite = tblCompositeLU.ID) LEFT JOIN tblConditionLU ON tblClientSignAuditDetail.Condition = tblConditionLU.ID) ON tblClientSignAudit.ID = tblClientSignAuditDetail.ClientAuditID
WHERE (((tblTypeLU.Code)=[Forms]![frmSearchForClientAuditForReport]![txtCode]) AND ((tblClientSignAuditDetail.ClientAuditID)=[Forms]![frmSearchForClientAuditForReport]![cboSearch]))
ORDER BY tblTypeLU.Code;

Open in new window


This is working fine.  But now I want the user to be able to print all of the reports, one after another, without selected the "txtCode" from a combo-box.  Just cycle through them and print them.  In some cases there may not be records with a particular code.  In that case, just skip over that type and go on to the next.

How can I do this with VBA code?
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

I think you need something like this :
 DoCmd.OpenReport NameOfYourReport, acViewPreview, , Criteria
                    DoCmd.SelectObject acReport, NameOfYourReport
                    DoCmd.PrintOut , , , , NoOfCopies
                    DoCmd.Close acReport, NameOfYourReport

Open in new window

Avatar of SteveL13

ASKER

I don't see the looping part of the code.  ???
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Modify the report to do a page break on txtCode if it doesn't do one already.

Change the query to select all records if [Forms]![frmSearchForClientAuditForReport]![txtCode] is empty.
SELECT tblClientSignAudit.ClientName, tblClientSignAudit.ClientAddress, tblClientSignAuditDetail.Location, tblClientSignAuditDetail.SiteImageFolder, tblTypeLU.Code, [SizeWidth] & "x" & [SizeHeight] & "x" & [SizeDepth] AS [Size], tblCompositeLU.Description AS Composition, tblConditionLU.Description AS Condition, tblClientSignAuditDetail.Electric, tblClientSignAuditDetail.Message, tblClientSignAuditDetail.Qty, tblClientSignAuditDetail.ClientAuditID, tblClientSignAuditDetail.TagNumber, tblTypeLU.Description, tblAuditTypesLU.Description, tblClientSignAudit.LogoFile
FROM (tblClientSignAudit LEFT JOIN tblAuditTypesLU ON tblClientSignAudit.SheetDescription = tblAuditTypesLU.ID) INNER JOIN (((tblClientSignAuditDetail LEFT JOIN tblTypeLU ON tblClientSignAuditDetail.Type = tblTypeLU.ID) LEFT JOIN tblCompositeLU ON tblClientSignAuditDetail.Composite = tblCompositeLU.ID) LEFT JOIN tblConditionLU ON tblClientSignAuditDetail.Condition = tblConditionLU.ID) ON tblClientSignAudit.ID = tblClientSignAuditDetail.ClientAuditID
WHERE (tblTypeLU.Code = [Forms]![frmSearchForClientAuditForReport]![txtCode] OR [Forms]![frmSearchForClientAuditForReport]![txtCode] Is Null) AND tblClientSignAuditDetail.ClientAuditID=[Forms]![frmSearchForClientAuditForReport]![cboSearch]
ORDER BY tblTypeLU.Code;

Open in new window

Pat,

The page numbering has to start with "1" for each report.  Therefore the code has to somehow loop through all codes and run each report one after another.  Am I making any sense at all?

--Steve
So since I can't have criteria for txtCode this is my record source for the report.

SELECT tblClientSignAudit.ClientName, tblClientSignAudit.ClientAddress, tblClientSignAuditDetail.Location, tblClientSignAuditDetail.SiteImageFolder, tblTypeLU.Code, [SizeWidth] & "x" & [SizeHeight] & "x" & [SizeDepth] AS [Size], tblCompositeLU.Description AS Composition, tblConditionLU.Description AS Condition, tblClientSignAuditDetail.Electric, tblClientSignAuditDetail.Message, tblClientSignAuditDetail.Qty, tblClientSignAuditDetail.ClientAuditID, tblClientSignAuditDetail.TagNumber, tblTypeLU.Description, tblAuditTypesLU.Description, tblClientSignAudit.LogoFile
FROM (tblClientSignAudit LEFT JOIN tblAuditTypesLU ON tblClientSignAudit.SheetDescription = tblAuditTypesLU.ID) INNER JOIN (((tblClientSignAuditDetail LEFT JOIN tblTypeLU ON tblClientSignAuditDetail.Type = tblTypeLU.ID) LEFT JOIN tblCompositeLU ON tblClientSignAuditDetail.Composite = tblCompositeLU.ID) LEFT JOIN tblConditionLU ON tblClientSignAuditDetail.Condition = tblConditionLU.ID) ON tblClientSignAudit.ID = tblClientSignAuditDetail.ClientAuditID
WHERE (((tblClientSignAuditDetail.ClientAuditID)=[Forms]![frmSearchForClientAuditForReport]![cboSearch]))
ORDER BY tblTypeLU.Code;

Open in new window


So, how do I loop through the reports to print them one after another?
@steve

seen my post at http:#a40976157 ?

to send the reports to the printer,
change
 DoCmd.OpenReport rptName, acViewPreview

with

 DoCmd.OpenReport rptName, acViewNormal
or simply

 DoCmd.OpenReport rptName
print all of the reports, one after another, without selected the "txtCode" from a combo-box.
Loop the txtCode values
I will presume that they are stored in a table (ex.: txtCodes)
I will also presume that the codes are all numeric

Dim strReportName As String
Dim rstCodes As DAO.Recordset
strReportName = "YourReportName"
Set rstCodes = CurrentDb.OpenRecordset("SELECT Code FROM tblCodes")
rstCodes.MoveFirst
Do Until rstCodes.EOF
    'Test
    'MsgBox rstCodes!Code
    DoCmd.OpenReport strReportName, , , "Code=" & rstCodes!Code, acHidden
    DoCmd.Close acReport, strReportName
    rstCodes.MoveNext
Loop
MsgBox "Done"

Open in new window


JeffCoachman
As long as you don't need page x of y, you can initialize page in the Format event of the header or footer (whichever holds the page number).
As far I can tell..., the SQL is mostly irrelevant here.

From what I can tell, you need to loop the codes and print a report for each Code.
Correct?
This is what my code does.

If this is not what you need then you will have to be a bit clearer as to what you have, and the exact results you are expecting...

My guess is that after a fuller explanation, ...any of us can get you sorted...

JeffCoachman
Jeffrey:  This is very close.  But codes are text.  How would this change:

Dim strReportName As String
Dim rstCodes As DAO.Recordset
strReportName = "YourReportName"
Set rstCodes = CurrentDb.OpenRecordset("SELECT Code FROM tblCodes")
rstCodes.MoveFirst
Do Until rstCodes.EOF
    'Test
    'MsgBox rstCodes!Code
    DoCmd.OpenReport strReportName, , , "Code=" & rstCodes!Code, acHidden
    DoCmd.Close acReport, strReportName
    rstCodes.MoveNext
Loop
MsgBox "Done"

Open in new window

I'm not sure why acHidden is being used and since the report is going directly to print, there is no need to close it.

docmd.OpenReport strReportName, acViewNormal, , "Code= '" & rstCodes!Code & "'"

AcViewNormal is the default (direct to the printer) so you can omit it.
...just old lazy syntax Pat....
Just threw it together quickly as an example...
The code should work as posted, ...I just wanted to see if this is the loop logic the OP wanted...
...Then the code could be refined as needed.

Jeff
Here is what I have so far.  But it is only printing the 1st report.

Dim strReportName As String
Dim rstCodes As DAO.Recordset
    strReportName = "Report2-Original"
    Set rstCodes = CurrentDb.OpenRecordset("SELECT Code FROM tblTypeLU")
    rstCodes.MoveFirst
    Do Until rstCodes.EOF
       
        'To go directly to printer...
        DoCmd.OpenReport strReportName, acViewNormal, , "Code= '" & rstCodes!Code & "'"

        DoCmd.Close acReport, strReportName
        rstCodes.MoveNext
    Loop
    
    MsgBox "Done"

Open in new window

@steve

did you ever look at the codes i posted?
Yes,
Steve, please first reply to Rey
Steve,
Put a stop in the code and step through the loop.  Examine the  contents of rstCodes!Code

Remove the line that closes the report.  It isn't necessary and may be causing a problem since at the time it runs, the report isn't open.