Link to home
Start Free TrialLog in
Avatar of rodneygray
rodneygrayFlag for United States of America

asked on

Print record after adding

I am printing barcode labels .  When the program starts a form appears that lets the user select a customer. After customer is selected, I close that form and open another form. I used the after insert event to print the labels. However, when I get the printout, a main form opens with a subform. The main form has customer information. The subform has item information. After I enter an item, I print a barcode. The barcode has the customer information and the information from the item just entered. I fire the print job from the After Insert event. The problem is that information from detail record does not print. I tried docmd.refreshquery
The strange thing is I can look at the connected subform database and the record is there. However, if I run a Query to extract that record it comes up blank. The code that prints the barcode is below
Private Sub Form_AfterInsert()
    Dim strReportName As String
    Dim strCriteria As String
    Dim strLpadBaleID As String
       
    'Leftpad with zeros
      strLpadBaleID = String(6 - Len(Me!txtBaleID), "0") & Me!txtBaleID
    'Format barcode field
      Me.txtBarcode = Me.txtCompanyID & strLpadBaleID
    'Report Selection    
      strReportName = "rptBarcode"
    'Set Criteria
       strCriteria = "[InvoiceNumber] = " & Me.txtInvoiceNumber & " and [BaleID]= " & Me.txtBaleID
    'Open report using criteria
      DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
End sub
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Try adding a refresh before running the report:

Private Sub Form_AfterInsert()
    Dim strReportName As String
    Dim strCriteria As String
    Dim strLpadBaleID As String
       
    'Leftpad with zeros
      strLpadBaleID = String(6 - Len(Me!txtBaleID), "0") & Me!txtBaleID
    'Format barcode field
      Me.txtBarcode = Me.txtCompanyID & strLpadBaleID
    'Report Selection    
      strReportName = "rptBarcode"
      Me.refresh '<----- Add this
    'Set Criteria
       strCriteria = "[InvoiceNumber] = " & Me.txtInvoiceNumber & " and [BaleID]= " & Me.txtBaleID
    'Open report using criteria
      DoCmd.OpenReport strReportName, acViewPreview, , strCriteria
End sub
Avatar of rodneygray

ASKER

I tried the refresh. No go. What I finally did was to extract all required data into variables and passed them to the printing program. Then I used split to grab the arguments in the OnLoad event.
--------------------------------------------------------------------
Form code
---------------------------------------------------------------------
Private Sub Form_AfterInsert()
    Dim strReportName As String
    Dim strCriteria As String
    Dim strLpadBaleID As String
    Dim strPounds As String
    Dim strMoisture As String
    Dim strGrade As String
    Dim strBarcode As String
    Dim strOpenArgs As String
       
    strPounds = Me.txtPounds
    strMoisture = Me.txtMoisture
    strGrade = Me.cmboGrade
    strLpadBaleID = String(6 - Len(Me!txtBaleID), "0") & Me!txtBaleID
    Me.txtBarcode = Me.txtCompanyID & strLpadBaleID
    strBarcode = Me.txtBarcode
   
    strOpenArgs = strPounds & ";" & strMoisture & ";" & strGrade & ";" & strBarcode
   
    strReportName = "rptBarcodeTest"
    strCriteria = "[InvoiceNumber] = " & Me.txtInvoiceNumber & " and [BaleID]= " & Me.txtBaleID
    DoCmd.OpenReport strReportName, acViewPreview, , strCriteria, , strOpenArgs

------------------------------------------------------------------------
Report Code
------------------------------------------------------------------------
Private Sub Report_Load()
    Dim x
    Dim strPounds As String
    Dim strMoisture As String
    Dim strGrade As String
    Dim strBarcode As String
    Dim strOpenArgs As String
   
    x = Split(OpenArgs, ";")
   
    strPounds = x(0)
    strMoisture = x(1)
    strGrade = x(2)
    strBarcode = x(3)
   
    Me.txtPounds = strPounds & " Lbs"
    Me.txtPounds = strPounds2 & " Lbs"
    Me.txtMoisture = strMoisture
    Me.txtMoisture2 = strMoisture
    Me.txtGrade = strGrade
    Me.txtGrade2 = strGrade
    Me.txtBarcode = strBarcode
    Me.txtBarcode2 = strBarcode
    Me.txtBarcode3 = strBarcode
    Me.txtBarcode4 = strBarcode
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
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
Thought issue was resolved. However, even when passing variables to report, data from record just added does not print when going to printer. However, if I preview the report, it prints correctly every time. Any way to cause print preview to print and then close preview window?
<However, even when passing variables to report, data from record just added does not print when going to printer.>
Again, try making sure the record is saved with the Dirty Event

<Any way to cause print preview to print and then close preview window?>
Then you would need a delay in order to actually hold the report open in order to review it. correct...?

These should probably be posted as new separate questions then...
boag2000: I would want the report to close immeditely. However, your me.dirtry statement did the trick.
<I would want the report to close immeditely>
    DoCmd.Close acReport, "YourReportName"