• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

Using an aggregate function in another form without making it read only


I have used an aggregate function to count the total number of entries for each pupil in a table. I then wish to use that total filed in another query but when I join the queries together I am not able to edit any of the fields in the original query. How can I show this total and still edit data. I also tried a manual update of the data to a field in the table but it will not let me do that either.

Any help appreciated.

  • 3
  • 2
1 Solution
Rather than joining the two queries, you can use a DLookup on the form that needs to display the total.

Add a textbox and set it's control source property to (include the = sign):

= DLookup("YourTotalField", "YourQueryThatCalculatesTheTotal")

See Jim Dettman's article about DLookup here:

With that approach, you can display the total without affecting the query that you need to remain editable.
Also, depending on the recordsource of the form that you need to display the total on....

If the total is based on records *in the form's recordsource*, you can add a textbox to the header or footer section of the form, and use an aggregate function as it's control source:

= SUM(SomeFieldName)

"SomeFieldName" needs to be the name of a field, as it appears in the underlying table... not the name of a textbox on the form.
tmckeatingAuthor Commented:
Ok so syntax of these things always throws me I see from your link I have to use something like this

 =Dlookup("[Name]","[tblCompanies]","[CompanyID] =  " & gCurrentCompany)

So if the field name is Total contact and the query is called Countbypupil but its on a continuous form and I only want to return the count for the record that I am currently on, of which the key field is PupilID

=Dlookup([Total],[Countbypupil],[PupilID]= me.[PupilID]    

do I need " " and how do I get it just to return the total for the current pupil?

tmckeatingAuthor Commented:
Tried this

=DLookUp("[total]","[count by pupil]","[Pupil id] = "" & Me.[pupil id] & "")

says expression is an invalid string
it works with

=DLookUp("[total]","[count by pupil]") but obviously returns the same values for all records.
You're close with the first expression... just  too many quotes.

IF PupilID is numeric:

=DLookUp("[total]","[count by pupil]","[Pupil id] = " & [pupil id])

Open in new window

If it is TEXT:

=DLookUp("[total]","[count by pupil]","[Pupil id] = '" & [pupil id] & "'")

Open in new window

Also note that I have dropped the "ME" prefix, which is a VBA term and does not work in property sheets or queries.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now