PDA

View Full Version : Loop not looping



TPL
06-09-2007, 06:54 AM
My process is running twice but it is not moving to the next record. I confess, I don't 'loop' very well. Any ideas? My record set is just two reports to start with:
rtReportstoPrintRepNumberReportNameRepDate1tCustomers6/1/20072tCustomersTest6/2/2007

It is running report 1 twice. . . . any help would be GREATLY appreciated. Thank you. I am trying to adapt Ken Puls excel pdfcreator code to access for outputting multiple reports with code. . .


Function ReportToPDFMultiple_Early()
'Author : Ken Puls ( )
'Macro Purpose: Print to PDF file using PDFCreator
' (Download from )
' Designed for early bind, set reference to PDFCreator

Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim sPrinterName As String
Dim sReportName As String
Dim lPrinters As Long
Dim lPrinterCurrent As Long
Dim lPrinterPDF As Long
Dim prtDefault As Printer

Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Dim strDate As String
Dim strRep As String
Dim strNum As Long

Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("rtReportstoPrint")
strDate = MyRS!repdate
strRep = MyRS!reportname
strNum = MyRS!repnumber

'Resolve index number of printers to allow changing and preserving
sPrinterName = Application.Printer.DeviceName
On Error Resume Next
For lPrinters = 0 To Application.Printers.Count
Set Application.Printer = Application.Printers(lPrinters)
Set prtDefault = Application.Printer

Select Case prtDefault.DeviceName
Case Is = sPrinterName
lPrinterCurrent = lPrinters
Case Is = "PDFCreator"
lPrinterPDF = lPrinters
Case Else
'do nothing
End Select
Next lPrinters
On Error GoTo 0

'Change the default printer
Set Application.Printer = Application.Printers(lPrinterPDF)
Set prtDefault = Application.Printer

'Start PFF Creator
Set pdfjob = New PDFCreator.clsPDFCreator
sPDFPath = "C:\Documents and Settings\Tami-L4C\My Documents\backups"

If pdfjob.cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Function
End If

For strNum = 1 To 2
If strNum > 0 Then
With pdfjob
'/// Change the report and output file name here! ///
'sReportName = strRep
sPDFName = strRep & strDate & ".pdf"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With

'Print the document to PDF
DoCmd.OpenReport (strRep)

'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False

'Wait until the PDFCreator queue is clear
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
End If
Next strNum

pdfjob.cClose
'Reset the (original) default printer and release PDF Creator
Set Application.Printer = Application.Printers(lPrinterCurrent)
Set pdfjob = Nothing
End Function


Edited 12-Jun-07 by geekgirlau. Reason: insert vba tags

WinteE
06-10-2007, 05:14 AM
tpl,

You better start using tabs when you're writing code :


For i = 1 To 10
varB = varB +1
Next i

... instead of ...


For i = 1 To 10
varB = varB +1
Next i

It will make it easier to read what you're code is going to do.

Erik

asingh
06-10-2007, 06:32 AM
give this a try:

could not test it though..



For strNum = 1 To 2

With pdfjob

'/// Change the report and output file name here! ///
'sReportName = strRep

sPDFName = strRep & strDate & ".pdf"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With

'Print the document to PDF
DoCmd.OpenReport (strRep)

'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop

pdfjob.cPrinterStop = False

'Wait until the PDFCreator queue is clear

Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop

Next strNum



I doubt you need an if statement if strNum > 0, because the FOR loop condition takes care of that.....!

regards,

asingh

geekgirlau
06-12-2007, 01:04 AM
Welcome to the Board, TPL :006:

Just a quick note: when you are posting code, be sure to use the vba tags as it makes it easier to read. When you copy your code in, select the text then click on the "VBA" button.

alimcpill
06-20-2007, 03:15 AM
You don't actually move through your recordset as far as I can see (unless I've missed something).

Try this (although I haven't tested it):

change

For strNum = 1 To 2
If strNum > 0 Then
With pdfjob
'/// Change the report and output file name here! ///
'sReportName = strRep
sPDFName = strRep & strDate & ".pdf"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With

'Print the document to PDF
DoCmd.OpenReport (strRep)

'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False

'Wait until the PDFCreator queue is clear
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
End If
Next strNum

to


Do Until MyRS.EOF
'Set these variables here in the actual loop
strDate = MyRS!repdate
strRep = MyRS!reportname
strNum = MyRS!repnumber 'In fact it doesn't look like you need this variable
With pdfjob
'/// Change the report and output file name here! ///
'sReportName = strRep
sPDFName = strRep & strDate & ".pdf"
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With

'Print the document to PDF
DoCmd.OpenReport (strRep)

'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False

'Wait until the PDFCreator queue is clear
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop

MyRS.MoveNext
Loop