Go Premium for a chance to win a PS4. Enter to Win

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
?
892 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 40

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

972 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