Results 1 to 11 of 11
  1. #1
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38

    Print only selected record of a sub form

    I have a
    data entry form
    which consists of three forms


    1-
    Main
    form (customer) 2- Sub form (order) 3- Sub form (order details)
    the order details sub form is
    linked
    to order sub form, also the order sub form is linked to the
    customer form
    . I created a report by wizard with customer record set, and then dragged the order records to the report by using "from existing table". Finally, i created a sub report for the order details. From the data entry form, when i click the order sub form record, the corresponding fields of the order details shown only, and that fits my need. Well, I created a command button to print the selected records. I built. The command button has the following code

    Code:
    Private Sub cmdPrint_Click()
    
    
    Dim
    strWhere As String If Me.Dirty Then 'Save any edits. Me.Dirty = False End If If Me.NewRecord Then 'Check there is a record to print MsgBox "Please select a record" Else strWhere = "[CustomerID] = " & Me.[CustomerID] DoCmd.OpenReport "rptcustomers", acViewPreview, , strWhere End If End Sub

    However, when i select the the second row of the order in the data entry form which is refer to the second order of a customer, the report show all the orders and their details of that customer. I want only the selected order and its details printed. Can any help please. I explained what i did step by step just to be clear to the reader.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Is the command button on the main form or subform?

    You need to include the OrderID in the criteria, not the CustomerID
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    Is the command button on the main form or subform?

    You need to include the OrderID in the criteria, not the CustomerID
    The command button in the main form

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Try:

    strWhere = "[OrderID] = " & Me.subformcontainername.[OrderID]
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    Try:

    strWhere = "[OrderID] = " & Me.subformcontainername.[OrderID]
    I tried it , it shows me this error
    Click image for larger version. 

Name:	Untitled.jpg 
Views:	14 
Size:	21.4 KB 
ID:	16429

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Tell us what happens when you hit the Debug Button. Post the latest code you are using.

  7. #7
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by ItsMe View Post
    Tell us what happens when you hit the Debug Button. Post the latest code you are using.
    this is the code that I'm used

    Private Sub Command28_Click()
    'DoCmd.OpenReport "rptcustomers", acViewPreview, , "[CustomerID] = " & [CustomerID]

    Dim strWhere As String

    If Me.Dirty Then 'Save any edits.
    Me.Dirty = False
    End If

    If Me.NewRecord Then 'Check there is a record to print
    MsgBox "Please select a record"
    Else
    strWhere = "[OrderID] = " & Me.ordersubform.[OrderID]
    DoCmd.OpenReport "rptcustomers", acViewPreview, , strWhere
    End If
    End Sub

    and this is what ahappend when i pressed debug

    Click image for larger version. 

Name:	Untitled.jpg 
Views:	12 
Size:	52.2 KB 
ID:	16431

  8. #8
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    Does this works ?
    Code:
    strWhere = "[OrderID] = " & Me.ordersubform.Form![OrderID]

  9. #9
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by amrut View Post
    Does this works ?
    Code:
    strWhere = "[OrderID] = " & Me.ordersubform.Form![OrderID]
    Amazing, but it works partially, it returns the right order but with no order details.
    Note, I have another sub form contains the corresponding details of each order and have to appear with selected order

  10. #10
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by amrut View Post
    Does this works ?
    Code:
    strWhere = "[OrderID] = " & Me.ordersubform.Form![OrderID]
    Sorry Armut,
    yessssssss, your code works, Thanks alot

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by falahsalih View Post
    Amazing, but it works partially, it returns the right order but with no order details.
    Note, I have another sub form contains the corresponding details of each order and have to appear with selected order
    I was going to suggest to add more criteria to your strWhere but it sounds like your subreport is not using the correct Link Main/Child Fields property. Perhaps include OrderID as the only field or add it to the properties as a compound key to work with the existing Main Child Link.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 02-11-2013, 04:43 PM
  2. Replies: 3
    Last Post: 03-03-2012, 03:54 PM
  3. How to Print Selected Record
    By indranx in forum Reports
    Replies: 2
    Last Post: 05-31-2011, 09:54 PM
  4. Replies: 7
    Last Post: 02-25-2010, 12:32 PM
  5. Loading the form with selected record
    By emilylu3 in forum Access
    Replies: 1
    Last Post: 12-09-2005, 07:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums