Results 1 to 9 of 9
  1. #1
    jesterII is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    8

    VBA Code to Filter a Report from a Subform

    Hello friends,



    I have two tables, one called Company Profile and the second called Contact Persons. The field "Company Name" exists in both, each contact person belongs to a company that is in the Company Profile table (one-to-many relationship with Contact Persons as many).

    I created a Form-Subform. What I want to happen is that on my subform (Contact Persons), I have a 'generate record' button that will show me a report of the all the Contact Persons that belong to the same Company X.

    I tried doing this

    DoCmd.OpenReport "rcdContacts", , "[Company Name] = " & Me.Company_Name

    but the report is not filtered and contains records of all contact persons, not the ones in Company X. Please advise.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I think you have it in the wrong position, and if it's really a name you need the text syntax:

    BaldyWeb wherecondition
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jesterII is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    8
    Dear pbadly,
    Thanks for your fast reply. Here is what I have now

    DoCmd.OpenRecord "rcdContacts", , , "[Company Name] = '" & Me.[Company_Name] & "'"

    but this gives me a compiler error "Method or data member not found", although the Field [Company_Name] surely exists in my subform.

    Any thoughts?

  4. #4
    jesterII is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    8
    Sorry I meant to say

    DoCmd.OpenReport "rcdContacts", , , "[Company Name] = '" & Me.[Company_Name] & "'"

    yields the error Microsoft Access can't find the field '|1' referred to in your expression.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I note that one has a space and the other an underscore. Is that correct? The error implies that something is misspelled. Can you post the db here if that's not it?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    jesterII is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    8
    That does not seem to be the problem. It appears that my database exceeds the limit to be uploaded here. Do you have an idea on what to do next?

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Did you compact/repair then zip? That will get most under the limit. If not, you can email to

    pbaldy
    gmail
    com
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    jesterII is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    8
    It worked! I traced it back to an issue in my one-to-many relationship, which I ended up just deleting. It had no effect on the Master/Child relationship in my form/subform, and I edited the Query Statement such that they are still linked by Company Name.

    I was doubting something with the VBA code but it is indeed intact. Thank you.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Glad you got it sorted out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 05-05-2012, 08:43 AM
  2. How to filter dates using an apply filter code.
    By Jgreenfield in forum Reports
    Replies: 4
    Last Post: 11-15-2011, 01:38 PM
  3. Code to combine report filter and date range
    By rhubarb in forum Reports
    Replies: 17
    Last Post: 10-28-2011, 03:08 PM
  4. Pass Subform filter to subform in report
    By camftm in forum Programming
    Replies: 16
    Last Post: 07-19-2011, 07:12 AM
  5. Pass subform filter to a report
    By dinorbaccess in forum Reports
    Replies: 3
    Last Post: 01-10-2011, 05:34 PM

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