Possible to use DSum in the body of a continuous form ?

Posted on 2015-01-02
Last Modified: 2015-01-02
-I am using Access 2013 and have a query-based continuous form listing project information, including the  fields “PaymentType” and “PaymentAmount”.
-Each project is assigned one payment type, “A” or “B”.
-Payment amounts are stored in two separate tables: Tbl_PaymentsA and Tbl_PaymentsB (separate tables are necessary)
-The continuous form and two tables share a common key “ProjectID”.
-On the continuous form, the payment type information comes from the underlying query and the Payment Amount field is unbound.
-I am trying to work VBA code so that if the payment type is “A”, then the “Payment Amount” field displays the sum of the amount in Tbl_PaymentsA for that ProjectID, but if B, then the sum of the amount in Tbl_PaymentsB.
-I have tried various pieces of code without any luck, for example:

If Me.PaymentType = “A” Then Me.PaymentAmount = DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ &[Forms]![ContinuousForm]![ProjectID])


Me.PaymentAmount = DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ &[Forms]![ContinuousForm]![ProjectID])

End If

 (In the above case the “Payment Amount” field is blank)

Is what I am trying to do even possible ? If yes, any advice on the code would be much appreciated. If not, could anyone advise on the correct approach to this ?

Thank you in advance for any help.
Question by:Paul McCabe
  • 2
  • 2
LVL 24

Expert Comment

by:Phillip Burton
ID: 40527361
What you are trying to do will not work - but a minor modification might.

You are trying to set a textbox (I think) for a continuous field, but have it different for different rows. That can't be done in the way you are doing it in VBA. It will set all of the boxes like that.

Instead, why not use your Query or your Form to generate it directly, e.g.

PaymentAmount: IIf(PaymentType = “1”, DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ & [ProjectID], DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ & [ProjectID]))

Open in new window

or even

PaymentAmount: DSum(“Payment”, IIf(PaymentType = “1”, “Tbl_PaymentsA”, “Tbl_PaymentsB”), “ProjectID = “ & [ProjectID])

Open in new window

Note that there are two i in IIF. Hope this helps.
LVL 84
ID: 40527698
Phillip has you on the right track (i.e. do the work in the query, not the form) but to add a bit more:

A Continuous or Datasheet form will display correct values in Bound controls, but not unbound controls. If you try to set the value of unbound controls, Access will display the same value for all controls (generally the last value you tried to set, but it could differ).

The only way to show distinct values for each record is to bind the control (i.e. set a ControlSource) and then fill the data needed in the underlying query.

Author Comment

by:Paul McCabe
ID: 40527942
Phillip and Scott,

Thank you very much for your responses. I tweaked the first piece of code slightly ("PaymentType" enclosed in square brackets and changed "1" to "A") and made it the control source of a text box on the Continuous form; this correctly displays the payment amounts:
=IIf([PaymentType] = “A”, DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ & [ProjectID], DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ & [ProjectID]))

However, it is a bit slow, and as Scott suggests, it might be best to do the calculations in the query. I tried this but ran into a new problem; 9 seperate tables feed the query, and several of them have "ProjectID" and "PaymentType" fields. When I try to run the query, Access throws up a message telling me that multiple tables have these fields, so I guess I need to specify the table the query needs to reference. The table I want to reference is Tbl_ProjectMain, so I tried the following code:
PaymentAmount:IIf([Tbl_ProjectMain].[PaymentType] = “A”, DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID], DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID]))
The query executes alright, but the "PaymentAmount" column just says "error". Any ideas as to where I am going wrong here ? Also, just in case, I take it I don't have to include Tbl_PaymentsA or Tbl_PaymentsB in the query itself ?
LVL 24

Accepted Solution

Phillip Burton earned 500 total points
ID: 40527944
I would say try splitting it into three columns, and see where the error is:

Column1:[Tbl_ProjectMain].[PaymentType] = “A”
Column2: DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID])
Column3: DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID])

However, you have a missing ). You have:

PaymentAmount:IIf([Tbl_ProjectMain].[PaymentType] = “A”, DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID], DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID]))

It should read:

PaymentAmount:IIf([Tbl_ProjectMain].[PaymentType] = “A”, DSum(“Payment”, “Tbl_PaymentsA”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID]), DSum(“Payment”, “Tbl_PaymentsB”, “ProjectID = “ & [Tbl_ProjectMain].[ProjectID]))

Author Comment

by:Paul McCabe
ID: 40528005
The error was in Column 1, which should have read: Column1:[Tbl_ProjectMain].[PaymentType] = 1
It now works perfectly. Thank you so much for your help !

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In the article entitled Working with Objects – Part 1 (, you learned the basics of working with objects, properties, methods, and events. In Work…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now