runtime error 2448 you cant assign a value to this object

前端 未结 1 1884
清酒与你
清酒与你 2021-01-15 00:48

I am using David-W-Fenton\'s answer to this question to try to allow users to print a report when they click on a control, but I am getting the following error message:

相关标签:
1条回答
  • 2021-01-15 01:29

    The code posted cannot work, because when a form is opened as a dialog:

    DoCmd.OpenForm "dlgPrinter", , , , , acDialog, strReport
    

    no other logic after this line is executed until the form has been closed, at which point control returns to the next line after this one, and logic continues on - except of course you can no longer reference anything in that form, because it is now closed.

    Ok, now there is a question, where is the button the user clicks to print a report?

    What I'm thinking is to split the logic in PrintReport into two methods, one that launches it, and tells the form to configure itself (doing the stuff suggested in the comment), but the rest of PrintReport then needs to happen after the user clicks OK (or Cancel);

    So if I assume that you've got a form which can launch one or more reports, and the button is on this form, what I would suggest is this:

    In the click event for that button - no changes to what you've got.

    In that buttons form, add this:

    Public Sub DialogAccept()
        With Forms!dlgPrinter
            If .Tag <> "Cancel" Then
                Set Reports(strReport).Printer = Application.Printers((!cmbPrinter))
                Reports(strReport).Printer.Orientation = !optLayout
                Application.Echo False
                DoCmd.SelectObject acReport, strReport
                DoCmd.PrintOut acPages, !txtPageFrom, !txtPageTo
                PrintReport = True
            End If
        End With
        DoCmd.Close acForm, "dlgPrinter"
        DoCmd.Close acReport, strReport
        Application.Echo True
    End Sub
    

    Change PrintReport to:

    Public Function PrintReport(strReport As String, strVarName As String, numVal As Long) As Boolean
        ' open report in PREVIEW mode but HIDDEN
        DoCmd.OpenReport strReport, acViewPreview, , strVarName & " = " & numVal, acHidden
    
        'DoCmd.OpenReport strReport, acViewPreview, , , acHidden
        ' open the dialog form to let the user choose printing options
        DoCmd.OpenForm "dlgPrinter", , , , , , strReport
        Forms!dlgPrinter.Configure
    End Function
    

    In the OK/Cancel button click events on dlgPrinter put (after existing code, but removing any instances of "Docmd.close"):

    Forms!Calling_Form_Name.DialogAccept
    

    This then calls that method, to do the stuff that is meant to happen after the user says "I'm done with the dialog".

    Finally add the configure method to dlgPrinter:

    Public Sub Configure()
        With Reports(Me.Tag).Printer
            Me!cmbPrinter = .DeviceName
            Me!optLayout = .Orientation
        End With
        Dim numPages As String
        numPages = Reports(Me.Tag).Pages
        Debug.Print "numPages = " & numPages
        TypeName(Me.txtPageTo) 'added this line as a test after
        Me.txtPageTo.Value = numPages
    End Sub
    

    And remove this code section from Form_Load.

    Hoep this helps.

    Lastly, if the form on which the button launching dlgPrinter can vary, change this line:

    DoCmd.OpenForm "dlgPrinter", , , , , acDialog, strReport
    

    to:

    DoCmd.OpenForm "dlgPrinter", , , , , acDialog, strReport & ";" & me.Name
    

    In form_load of dlgPrinter, break up me.Openargs using left() & mid() with instr(), and save me.Name into the tag of something on the form (which you're not already using the tag of).

    In the OK/Cancel button click events, change the code above to:

    Forms(Object.Tag).DialogAccept
    
    0 讨论(0)
提交回复
热议问题