specify which report to print based on data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am working with a pre-existing database that has two different forms to
print (labels) depending on what kind of item they are printing the label
for. Each item has a letter code to define what type of item it is, and the
types are fixed (static list). The users currently have to pick either Label
A or Label B to print the correct label. My question is, how would I build
or modify the print event to specify "if type=B or C, print label 2, else
print label 1". Sorry for being simplistic, I get these thrown at me from
left field all the time. I am not a VB or Access programmer, so please be
kind with the responces :)
 
This should work, just make sure you if you copy and paste that the command
name matches your button name:

Private Sub Print_Click()
On Error GoTo Err_Print_Click
Dim stDocName As String
stDocName = "LabelB"
stDocName1 "LabelC"

If Me.Type = "B" or Me.Type = "C" Then
DoCmd.OpenReport stDocName, acNormal
Else
DoCmd.OpenReport stDocName1, acNormal
End If

Exit_Print_Click:
Exit Sub

Err_Print_Click:
MsgBox Err.Description
Resume Exit_Print_Click

End Sub

Hope this helps.

B
 
Hmmm, when I click the button, nothing happens now, it looks like you may
have a typo or two. don't I have to do a dim stDocName1 as String? Here is
what I modified...
Private Sub btnLayout_Click()
On Error GoTo Err_btnLayout_Click
Dim stDocName As String
Dim stDocName1 As String
stDocName = "rptLabel02"
stDocName1 = "rptLabel03"

If Me.Type = "C" Or Me.Type = "D" Then
DoCmd.OpenReport stDocName1, acNormal, , Me.OpenArgs
Else
DoCmd.OpenReport stDocName, acNormal, , Me.OpenArgs
End If
DoCmd.Close acForm, "frmLabelSel"

Exit_btnLayout_Click:
Exit Sub

Err_btnLayout_Click:
MsgBox Err.Description
Resume Exit_btnLayout_Click

End Sub
 
Ok, duh, the table wasn't associated with the report, not sure how it worked
in the past. Anyways, I can print now, but it doesn't seem to resolve the
Type properly. The field is Text, one character long. I am chosing records
that I know are Type C or D, but getting the other report.
 
Is the user selecting from a list with type a, b and c? Or is the program
supposed to know what type it is by the data entered? You may need a refresh
command before printing to since the field the print command is looking at
may be defaulting.

B
 
The user is selecting the record from a form, then clicking a function
(Label) to bring up the form to print the label (label selection screen).
The proper record is still being selected, however, the "if then" doesn't
seem to do anything. How am I defining what Me.Type is, or should that be
from the current record?
 
Me.Type should be the field the user is selecting from. Whatever name the
field, list box or combo box is, use as Me.FieldName.
 
Hmmm, I don't disagree (how can I, when you got me this far already!) I can
do a preview of the report (label) and it shows the Type field on it, but the
label selection itself continues to elude me. I can force the report by
changing the "if then else" statement, but that doesn't do me much good. I
can work around it by creating another print button, so I have one for each
report, but I would rather remove the human factor.
 
I am not sure why it won't work. Try the code without the "Or" statement
limiting the selection to only C or else:

If Me.Type = "C" then
Print form A
Else
Print form B
End If

Perhaps the or statement is not working correctly. Doing the above will
detemine if the statement itself is performing as expected. Do any errors
come up when running the code?

B
 
No errors, I tried your suggestion, I still get the same results, I get one
report, not the other. I attempted to simply the code, here is what I am
trying...

Private Sub Preview_Layout_Click()
On Error GoTo Err_Preview_Layout_Click
If Me.Type = "C" Then
DoCmd.OpenReport "rptLabel03", acPreview, , Me.OpenArgs
Else
DoCmd.OpenReport "rptLabel02", acPreview, , Me.OpenArgs
End If
DoCmd.Close acForm, "frmLabelSel"
Exit_Print_Layout_Click:

Exit_Preview_Layout_Click:
Exit Sub

Err_Preview_Layout_Click:
MsgBox Err.Description
Resume Exit_Preview_Layout_Click

End Sub
 
Back
Top