Solved

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

Posted on 2013-12-02
2
852 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
2 Comments
 
LVL 35

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

840 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