Results 1 to 8 of 8
  1. #1
    2 Star Lounger
    Join Date
    Mar 2001
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printing a form (Access XP)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a form (Access XP)

    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.

  3. #3
    2 Star Lounger
    Join Date
    Mar 2001
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a form (Access XP)

    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

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a form (Access XP)

    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)

  5. #5
    2 Star Lounger
    Join Date
    Mar 2001
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a form (Access XP)

    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
    Attached Files Attached Files

  6. #6
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Printing a form (Access XP)

    You need to change the code in the Preview Report button just a little:
    <pre>
    Dim stDocName As String
    stDocName = "Order"
    DoCmd.OpenReport stDocName, acPreview, , "[ID]=" & Me.[ID]
    </pre>

    Regards
    John



  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a form (Access XP)

    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.
    Attached Files Attached Files

  8. #8
    2 Star Lounger
    Join Date
    Mar 2001
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a form (Access XP)

    Thanks Hans and John,

    It works great.

    Bret

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •