Solved

How select all records using VBA code if field are empty in the criteria form

Posted on 2013-12-02
2
869 Views
Last Modified: 2013-12-06
I have the following very lengthy VBA code in an on print event of a report.  But there are two fields on the form frmSelectDateRangeAndMembers that if left blank by the user are to get all records for txtMonth and txtMemberName.

How would I change the code to accommodate this?

Here's the code:

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
   
    'Group 1 Reporting Starts Here...
    'Column 1 (Goal) code...
    Me.txtListingsTakenGoal = Nz(DLookup("[ListingsCount]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    Me.txtListingsSold = Nz(DLookup("[ListingsSold]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    Me.txtBuyersSold = Nz(DLookup("[BuyersSold]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    'End of column 1 (Goal) code

    'Column 2 (Listings) code...
    Me.txtCountListings = Nz(DCount("[ID]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [ListMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [ListYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    'End of column 2 (Listings) code

    'Column 3 (Pending) code...
    Me.txtListingsSoldPending = Nz(DCount("[ID]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Pending] = True"), 0)
    Me.txtBuyersSoldPending = Nz(DCount("[ID]", "tblTransactions", "[SellingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Pending] = True"), 0)
    'End of column 3 (Pending) code

    'Column 4 (Closed) code...
    Me.txtListingsSoldClosed = Nz(DCount("[ID]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Closed] = True"), 0)
    Me.txtBuyersSoldClosed = Nz(DCount("[ID]", "tblTransactions", "[SellingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Closed] = True"), 0)
    'End of column 4 (Closed) code
    'End of Group 1 Reporting


    'Group 2 Reporting Starts Here...
    'Column 1 (Goal) code...
    Me.txtCommBuyersSold = Nz(DLookup("[CommissionBuyersSold]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    Me.txtAgntLead = Nz(DLookup("[AgentLead]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    Me.txtCommSoldListingGoal = Nz(DLookup("[CommissionSoldListing]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
    'End of column 1 (Goal) code


    'Column 3 (Pending) code...
    'Me.txtLeadFromEdPend = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Pending] = True" And [LeadFrom] = "Ed"), 0)
    Me.txtLeadFromEdPend = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Pending] = True And [LeadFrom] = 'Ed'"), 0)
    Me.txtAgentLeadPending = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Pending] = True And [LeadFrom] = 'Agent'"), 0)
    Me.txtCommSoldListingPending = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Pending] = True"), 0)
    'End of column 3 (Pending) code


    'Column 4 (Closed) code...
    Me.txtLeadFromEdClsd = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Closed] = True And [LeadFrom] = 'Ed'"), 0)
    Me.txtLeadEdClosed = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Closed] = True And [LeadFrom] = 'Agent'"), 0)
    Me.txtCommSoldListingClosed = Nz(DSum("[Agent Sale $ Commission]", "tblTransactions", "[ListingAgent] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [PendingMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [PendingYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear & " And [Closed] = True"), 0)
    'End of column 4 (Closed) code
    'End of Group 1 Reporting

End Sub
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
PatHartman earned 500 total points
ID: 39690560
Selection criteria is applied BEFORE the report opens.  It needs to be in your query.  See my response in this post for a solution.
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_28307698.html

It is hard to read the code since it is not formatted well.  In the future, please use the Code tags to retain formatting.

If you want to modify this code, find the place where the argument is used and add an OR "Is Null" part to the expression.  I'll do one to get you going.

Me.txtListingsTakenGoal = Nz(DLookup("[ListingsCount]", "tblMemberGoals", "[TeamMember] = (" & Forms!frmSelectDateRangeAndMembers!txtMemberName & " OR frmSelectDateRangeAndMembers!txtMemberName Is Null) And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)

Open in new window


The other option is to add If statements and use different criteria for the DLookup()'s based on presence or absence in the search fields.

If IsNull(frmSelectDateRangeAndMembers!txtMemberName) Then
    Me.txtListingsTakenGoal = Nz(DLookup("[ListingsCount]", "tblMemberGoals", "[TeamMember] = " & Forms!frmSelectDateRangeAndMembers!txtMemberName & " And [GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
Else
    Me.txtListingsTakenGoal = Nz(DLookup("[ListingsCount]", "tblMemberGoals", "[GoalMonth] = " & Forms!frmSelectDateRangeAndMembers!txtMonth & " And [GoalYear] = " & Forms!frmSelectDateRangeAndMembers!txtYear), 0)
End If

Open in new window

0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39694235
If at all possible, it is better to do filtering and creation of calculated fields in a record source query, rather than in code on a report event.  It is much, much easier to test and debug.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question