Loop Problem

D

DS

I have this statement that keeps getting hung up on the loop. It says
that I must have a DO, which I have but I guess somethings in the wrong
place. Any help appreciated.
Thanks
DS

Dim PO As Integer

'GET HIGHEST PRINTER
Me.TxtNumber = Nz(DMax("PDPrinterID", "tblPrintDetails", "PDCheckID = "
& Me.TxtCheckID), 0)
On Error GoTo ErrorHandler
'THEN START LOOP
Do
'LOOK FOR EMPTY CHECK
Me.TxtEmpty = Nz(DCount("PDPrinterID", "tblPrintDetails", "PDPrinterID =
" & Me.TxtNumber & " " & _
"AND PDCheckID = " & Me.TxtCheckID), 0)
'IF CHECK IS EMPTY THEN REDUCE PRINTERID BY 1
If IsNull(Me.TxtEmpty) Or _
Me.TxtEmpty = 0 Then
Me.TxtNumber = Me.TxtNumber - 1
Else:
'IF CHECK NOT EMPTY THEN CHECK FOR CONDITIONAL
Me.TxtCon = Nz(DLookup("PDCon", "tblPrintDetails", "PDPrinterID = " &
Me.TxtNumber), 0)
If Me.TxtCon = -1 Then
'IF CONDITIONAL IS -1 THEN GET CURRENT ITEM NUMBER
Me.TxtItemID = Nz(DLookup("PDItemID", "tblPrintDetails", "PDPrinterID =
" & Me.TxtNumber), 0)
'THEN COUNT OTHER ITEMID'S WITH SAME PRINTERID AND SAME CHECKID
Me.TxtGo = Nz(DCount("PDItemID", "tblPrintDetails", "PDItemID <> " &
Me.TxtItemID & " And PDPrinterID = " & Me.TxtNumber & "And PDCheckID = "
& Me.TxtCheckID), 0)
'IF COUNT IS EQUAL OR GREATER THAN 1 THEN PRINT, OTHERWISE END
If Me.TxtGo >= 1 Then
Me.TxtName = Nz(DLookup("PrinterName", "tblPrinters", "PrinterID = " &
Me.TxtNumber), "")
If Not Me.TxtName = "No Print" Then
If Not Me.TxtName = "" Then
'IF NOT "NO PRINT" THEN PRINT!
Set Application.Printer = Application.Printers(Me.TxtName.Value)
DoCmd.OpenReport "rptTwo", , , "PDCheckID = " & Me.TxtCheckID & " And
PDPrinterID = " & Me.TxtNumber & ""
Me.TxtNumber = Me.TxtNumber - 1
Set Application.Printer = Nothing
End If
ElseIf Me.TxtGo = 0 Then
End If
ElseIf Me.TxtCon = 0 Then
Me.TxtName = Nz(DLookup("PrinterName", "tblPrinters", "PrinterID = " &
Me.TxtNumber), "")
If Not Me.TxtName = "No Print" Then
If Not Me.TxtName = "" Then
Set Application.Printer = Application.Printers(Me.TxtName.Value)
DoCmd.OpenReport "rptTwo", , , "PDCheckID = " & Me.TxtCheckID & " And
PDPrinterID = " & Me.TxtNumber & ""
Me.TxtNumber = Me.TxtNumber - 1
Set Application.Printer = Nothing
End If
End If
Loop Until Me.TxtNumber = 0
End
ErrorHandler:
DoCmd.OpenForm "frmMsgWarning"
Forms!frmMsgWarning!TxtMsg = "INVALID PRINTER"
End Sub
 
D

DS

DS wrote:
OK I'm Closer to the culprit...Its the line
If Me.TxtGo = 0 Then
Me.TxtNumber = Me.TxtNumber
End If

Here is the revised code:

Dim PO As Integer
'GET HIGHEST PRINTER
Me.TxtNumber = Nz(DMax("PDPrinterID", "tblPrintDetails", "PDCheckID = "
& Me.TxtCheckID), 0)
On Error GoTo ErrorHandler
'THEN START LOOP
Do
Me.TxtEmpty = Nz(DCount("PDPrinterID", "tblPrintDetails", "PDPrinterID =
" & Me.TxtNumber & " " & _
"AND PDCheckID = " & Me.TxtCheckID), 0)

If IsNull(Me.TxtEmpty) Or _
Me.TxtEmpty = 0 Then
Me.TxtNumber = Me.TxtNumber - 1
ElseIf Me.TxtEmpty >= 1 Then
Me.TxtCon = Nz(DLookup("PDCon", "tblPrintDetails", "PDPrinterID = " &
Me.TxtNumber), 0)

If Me.TxtCon = -1 Then
Me.TxtItemID = Nz(DLookup("PDItemID", "tblPrintDetails", "PDPrinterID =
" & Me.TxtNumber), 0)
Me.TxtGo = Nz(DCount("PDItemID", "tblPrintDetails", "PDItemID <> " &
Me.TxtItemID & " And PDPrinterID = " & Me.TxtNumber & "And PDCheckID = "
& Me.TxtCheckID), 0)

If Me.TxtGo >= 1 Then
Me.TxtName = Nz(DLookup("PrinterName", "tblPrinters", "PrinterID = " &
Me.TxtNumber), "")
If Not Me.TxtName = "No Print" Then
End If
If Not Me.TxtName = "" Then
End If
Set Application.Printer = Application.Printers(Me.TxtName.Value)
DoCmd.OpenReport "rptTwo", , , "PDCheckID = " & Me.TxtCheckID & " And
PDPrinterID = " & Me.TxtNumber & ""
Me.TxtNumber = Me.TxtNumber - 1
Set Application.Printer = Nothing
Else:
If Me.TxtGo = 0 Then
Me.TxtNumber = Me.TxtNumber - 1
End If

ElseIf Me.TxtCon = 0 Then
Me.TxtName = Nz(DLookup("PrinterName", "tblPrinters", "PrinterID = " &
Me.TxtNumber), "")
If Not Me.TxtName = "No Print" Then
End If
If Not Me.TxtName = "" Then
End If
Set Application.Printer = Application.Printers(Me.TxtName.Value)
DoCmd.OpenReport "rptTwo", , , "PDCheckID = " & Me.TxtCheckID & " And
PDPrinterID = " & Me.TxtNumber & ""
Me.TxtNumber = Me.TxtNumber - 1
Set Application.Printer = Nothing
End If
End If
Loop Until Me.TxtNumber = 0
End
ErrorHandler:
DoCmd.OpenForm "frmMsgWarning"
Forms!frmMsgWarning!TxtMsg = "INVALID PRINTER"
End Sub
 
T

Tom Lake

Try indenting under an If. You'll see your ELSEIFs are misplaced after End Ifs

Tom Lake
 
D

DS

Jeff said:
I've seen an error message like this when there were "unbalanced" If ... End
If statements inside a Do ... loop.
Got it, Basically the whole statement was a mess. I cleaned it up step
by step and it works.
Thanks
DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Code Is Slow 3
Is This Good? 13
Code Is Slow 6
RECORDSET 2
Complex If Statement 6
A Lot of Code 10
3rd grade code returns extra zero instead of decimal or hides deci 2
User form with VBA 10

Top