Solved

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

Posted on 2015-01-02
5
173 Views
Last Modified: 2015-01-02
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.
0
Comment
Question by:Paul McCabe
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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.
0
 
LVL 84
Comment Utility
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.
0
 

Author Comment

by:Paul McCabe
Comment Utility
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 ?
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
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]))
0
 

Author Comment

by:Paul McCabe
Comment Utility
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 !
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
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…
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 …
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now