Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-02
2
Medium Priority
?
889 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 39

Accepted Solution

by:
PatHartman earned 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

721 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