Printing Problems (1 Viewer)

Andy_CD

Bumbling Idiot
Local time
Today, 11:58
Joined
Feb 6, 2017
Messages
23
Hi All,
I am trying to use the following code to print out a report. And then based on if other parts are present print another report.
It all works beautifully, unless the second part is missing then it prints double the number of copies of the first report.
I have switched the code around, but when I do this it prints the number of copies required according to my settings, but it then also prints the input form.

I'm very new to this so have probably made the code more complicated than it needs to be. Any help/suggestions gratefully received!

Private Sub Command21_Click()

On Error Resume Next


DoCmd.RunCommand acCmdSaveRecord
If ([Forms]![Form Label BRI]![Drug].Column(1) = 0) Then
DoCmd.OpenReport "Label BRI", acViewPreview, "", "[Hospital Number]=[Hospital Number]", acNormal
DoCmd.SelectObject acReport, "Label BRI"
If [Forms]![Form Label BRI]![Pack or Dose Unit] = "Pack(s)" Then
DoCmd.PrintOut acSelection, , , , [Forms]![Form Label BRI]![Quantity].Value
Else: DoCmd.PrintOut acSelection, , , , , 1
End If
End If
If [Forms]![Form Label BRI]![Drug].Column(2) Like "*" Then
DoCmd.OpenReport "Label BRI Supp", acViewPreview, "", "[Hospital Number]=[Hospital Number]", acNormal
DoCmd.SelectObject acReport, "Label BRI Supp"
End If
If [Forms]![Form Label BRI]![Pack or Dose Unit] = "Pack(s)" Then
DoCmd.PrintOut acSelection, , , , [Forms]![Form Label BRI]![Quantity].Value
Else: DoCmd.PrintOut acSelection, , , , , 1
End If
DoCmd.Close acReport, "Label BRI Supp"
DoCmd.Close acForm, "Form Label BRI"
DoCmd.Close acReport, "Label BRI"



End Sub
 

Minty

AWF VIP
Local time
Today, 11:58
Joined
Jul 26, 2013
Messages
10,387
I've put your code in code tags and indented it - I've also changed the construct as I don't think your Else : syntax was correct.
Code:
 On Error Resume Next

    DoCmd.RunCommand acCmdSaveRecord
    
    If ([Forms]![Form Label BRI]![Drug].Column(1) = 0) Then
        DoCmd.OpenReport "Label BRI", acViewPreview, "", "[Hospital Number]=[Hospital Number]", acNormal
        DoCmd.SelectObject acReport, "Label BRI"
        If [Forms]![Form Label BRI]![Pack or Dose Unit] = "Pack(s)" Then
            DoCmd.PrintOut acSelection, , , , [Forms]![Form Label BRI]![Quantity].Value
        Else
            DoCmd.PrintOut acSelection, , , , , 1
        End If
    End If
    
    If [Forms]![Form Label BRI]![Drug].Column(2) Like "*" Then
        DoCmd.OpenReport "Label BRI Supp", acViewPreview, "", "[Hospital Number]=[Hospital Number]", acNormal
        DoCmd.SelectObject acReport, "Label BRI Supp"
    End If
    
    If [Forms]![Form Label BRI]![Pack or Dose Unit] = "Pack(s)" Then
        DoCmd.PrintOut acSelection, , , , [Forms]![Form Label BRI]![Quantity].Value
    Else
        DoCmd.PrintOut acSelection, , , , , 1
    End If

With it laid out like this I can't see anything obvious. Try the reformatted code?
 

Andy_CD

Bumbling Idiot
Local time
Today, 11:58
Joined
Feb 6, 2017
Messages
23
Hi,
Thanks for that, but it is still print the double copies. I think I need to make it drop the focus on the first report, before it sets off on the third if statement. Or this needs an else statement?
I think if the third if statement fails I need it to stop, before it gets to the forth if statement, but if it passes the third I need it to carry on to the forth.
Does this make sense or have I just waffled?
 

Minty

AWF VIP
Local time
Today, 11:58
Joined
Jul 26, 2013
Messages
10,387
No what you are saying makes sense. If you look at the indented code it highlights that your final statement Clause 3, will always print , but what it prints will be dependent on the result of first clause 1 then clause 2.

Code:
On Error Resume Next

    DoCmd.RunCommand acCmdSaveRecord
    
    If ([Forms]![Form Label BRI]![Drug].Column(1) = 0) Then [COLOR="Red"] 'Clause1[/COLOR]
        DoCmd.OpenReport "Label BRI", acViewPreview, "", "[Hospital Number]=[Hospital Number]", acNormal
        DoCmd.SelectObject acReport, "Label BRI"
        If [Forms]![Form Label BRI]![Pack or Dose Unit] = "Pack(s)" Then
            DoCmd.PrintOut acSelection, , , , [Forms]![Form Label BRI]![Quantity].Value
        Else
            DoCmd.PrintOut acSelection, , , , , 1
        End If
    End If
    
    If [Forms]![Form Label BRI]![Drug].Column(2) Like "*" Then  [COLOR="Red"] 'Clause2[/COLOR]
        DoCmd.OpenReport "Label BRI Supp", acViewPreview, "", "[Hospital Number]=[Hospital Number]", acNormal
        DoCmd.SelectObject acReport, "Label BRI Supp"
    End If
    
    If [Forms]![Form Label BRI]![Pack or Dose Unit] = "Pack(s)" Then [COLOR="Red"] 'Clause3[/COLOR]
        DoCmd.PrintOut acSelection, , , , [Forms]![Form Label BRI]![Quantity].Value
    Else
        DoCmd.PrintOut acSelection, , , , , 1
    End If
 

Andy_CD

Bumbling Idiot
Local time
Today, 11:58
Joined
Feb 6, 2017
Messages
23
Ah ha! So I am happy with clause 3 always printing, but only if I can make sure that clause 1 works only works if clause 2 is false.
Obviously what would be better is if Clause 1 always prints and clauses 2 and 3 only print if clause 2 is true.
 

Andy_CD

Bumbling Idiot
Local time
Today, 11:58
Joined
Feb 6, 2017
Messages
23
SOLVED Re: Printing Problems

Got this sorted! With thanks to Minty for tidying it up and explaining what was happening. I then worked out to a an extra if statement to end the event if needed.:)
 
Last edited:

Users who are viewing this thread

Top Bottom