• Printing a form (Access XP)

    Author
    Topic
    #383986

    I have created a database for taking orders. Once the order is taken there is a Print button on the form. The problem I have is getting only the current form to print. They way it is currently, every order we have ever taken prints out.

    TIA
    Bret

    Viewing 0 reply threads
    Author
    Replies
    • #656971

      The Print button probably executes an instruction DoCmd.Printout. If so, add acSelection so that it reads

      DoCmd.Printout acSelection

      This will make the button print the current record.

      Note: forms are meant for entering, editing and viewing data. Although they can be printed, they are not really designed for it. Reports are designed with printing in mind. You could create a report based on the same record source as the form, and then have the command button on the form open the report for the current record.

      • #656983

        Thanks Hans,
        There is a copy of the VB code after I changed it to what you suggested at the bottom of this message. It still prints all the records in the db. I would do as you suggest, and create a report. I what to keep it as simple and quick as possible. Is there a to make the report print from a button on the order form?

        Thanks again,

        Bret

        Private Sub Command20_Click()
        On Error GoTo Err_Command20_Click

        Dim stDocName As String
        Dim MyForm As Form

        stDocName = “Cake Order Form”
        Set MyForm = Screen.ActiveForm
        DoCmd.SelectObject acForm, stDocName, True
        DoCmd.PrintOut acSelection
        DoCmd.SelectObject acForm, MyForm.Name, False

        Exit_Command20_Click:
        Exit Sub

        Err_Command20_Click:
        MsgBox Err.Description
        Resume Exit_Command20_Click

        End Sub

        • #656995

          1. To make the button print just the current record, take out the two lines with DoCmd.SelectObject.

          2. In order to print the current record in a report, you must have a field (or combination of fields) that uniquely identifies a record. If the record source of the form has a primart key (for instance an AutoNumber field), you can use that. In the following example, I will assume that the record source has a numeric field OrderID that acts as unique identifier, and that you have a report named rptOrders, based on the same record source.

          The code for a command button cmdReport would look like:

          Sub cmdReport_Click()
          DoCmd.OpenReport “rptOrders”, acViewPreview, , “[OrderID]=” & Me.[OrderID]
          End Sub

          This will open the report in preview mode. If you’d rather send it directly to the printer, replace acViewPreview by acViewNormal.

          If OrderID is a text field, you must surround the value in quotes; to include a quote in a string in code, use Chr(34):

          … , [OrderID]=” & Chr(34) & Me.[OrderID] & Chr(34)

          • #657199

            Hans, I am still having difficulties with this printing all the orders in the database. I will attach the zipped database for you to look at if you would please. I did as you suggested before and created a report and the button previews that report. But when I print the report it prints all the orders.

            Thanks,

            Bret

            • #657203

              You need to change the code in the Preview Report button just a little:

                
                  Dim stDocName As String
                  stDocName = "Order"
                  DoCmd.OpenReport stDocName, acPreview, , "[ID]=" & Me.[ID]
              
            • #657204

              Bret,

              The reason that you get all records is that you haven’t added a where-condition to the DoCmd.OpenReport instruction. the code should look as follows:

              Dim stDocName As String, stLinkCriteria As String

              stDocName = “Order”
              stLinkCriteria = “ID=” & Me.ID
              DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria

              I have done this in the database you posted; the modified version is attached.

            • #657238

              Thanks Hans and John,

              It works great.

              Bret

    Viewing 0 reply threads
    Reply To: Printing a form (Access XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: