Link to home
Start Free TrialLog in
Avatar of Paul McCabe
Paul McCabe

asked on

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

Hello,
-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])

Else

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.
Avatar of Phillip Burton
Phillip Burton

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.
Avatar of Scott McDaniel (EE MVE )
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.
Avatar of Paul McCabe

ASKER

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 ?
ASKER CERTIFIED SOLUTION
Avatar of Phillip Burton
Phillip Burton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 !