473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Access query to count records which match another table

204 128KB
I have two related tables:
[Walks], which gives details such as date, location etc of a particular event; and
[Walk_participation_history], which records the people who attended that event. Fields include WalkNumber and PersonID.

I have now added a field NumPersons to the Walks table, and want to run a one-off query to populate the historical Walks records with the number of people who attended each Walk. I've tried a query using DCount but can't get it to work.

Any help appreciated!
May 29 '17 #1

✓ answered by PhilOfWalton

As I suspected, because WalkNumber is a text field, you need inverted commas, so try

Expand|Select|Wrap|Line Numbers
  1. DCount("PersonID","Walk_Participation_History",
  2. "WalkNumber = " & Chr$(34) & Walks.WalkNumber & Chr$(34))
  3.  
Also I suspect that the last line should check for Null and 0, so use:-

Expand|Select|Wrap|Line Numbers
  1. WHERE Nz(Walk_participation_history.Posn_ID) = 0;
  2.  
Phil

8 3855
PhilOfWalton
1,430 Expert 1GB
Ah, Petrol, I remember you query from about a year ago.

The DCount is probably the way to go.

What specifically have you tried? To the best of my recollection, WalkNumber is a Text field, so have you remembered to use the the double quotes round it (I prefer to use Chr$(34))

Phil
May 29 '17 #2
Petrol
204 128KB
You have a good memory!
I built an Access query based on the two tables, joined on WalkNumber (which is indeed a text field), and included "DCount("PersonID","Walk_Participation_History","W alk_Participation_History.WalkNumber = Walks.WalkNumber" as an expression. I built it as a Select query intending to change it to an Update query once I got it working, but it fails with the message "Emmaus BS DB cannot find the name "Walks.WalkNumber" you entered in the expression" - though that is certainly the correct name of the field.
The actual SQL generated by the query is as follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT Walk_participation_history.WalkNumber, Walks.WalkNumber, Walk_participation_history.Posn_ID, DCount("PersonID","Walk_Participation_History","Walk_Participation_History.WalkNumber = Walks.WalkNumber") AS Attendance
  2. FROM Walk_participation_history INNER JOIN Walks ON Walk_participation_history.WalkNumber = Walks.WalkNumber
  3. WHERE (((Walk_participation_history.Posn_ID)=0));
  4.  
May 29 '17 #3
PhilOfWalton
1,430 Expert 1GB
As I suspected, because WalkNumber is a text field, you need inverted commas, so try

Expand|Select|Wrap|Line Numbers
  1. DCount("PersonID","Walk_Participation_History",
  2. "WalkNumber = " & Chr$(34) & Walks.WalkNumber & Chr$(34))
  3.  
Also I suspect that the last line should check for Null and 0, so use:-

Expand|Select|Wrap|Line Numbers
  1. WHERE Nz(Walk_participation_history.Posn_ID) = 0;
  2.  
Phil
May 29 '17 #4
Petrol
204 128KB
Ah, that did it. Thank you so much.
WFYC is fortunate indeed.
Peter
May 29 '17 #5
NeoPa
32,556 Expert Mod 16PB
Hi Petrol.

Although a GROUP BY query, as well as a query that uses a GROUP BY subquery, become non-updatable, you can manage this in a simple UPDATE query such as below :
Expand|Select|Wrap|Line Numbers
  1. UPDATE [tblWalk]
  2.        INNER JOIN
  3.        [tblWalkHist]
  4.     ON [tblWalk].[WalkID]=[tblWalkHist].[WalkID]
  5. SET    [tblWalk].[NoPpl]=[NoPpl]+1
  6. WHERE  ([tblWalk].[NoPpl]=0)
I've used a WHERE clause to ensure you never recalculate a record that has already been calculated.

Domain Aggregate (such as DLookup(), DCount() etc.) functions are generally not recommended for use within SQL. There can be scenarios where they are a good solution, but they're few and far between.

NB. SQL standard for quotes is the single quote (') but both Jet & ACE support doubles (") too. For best portability and consistency with the standard I would always recommend the singles, but be reassured it shouldn't matter too much unless you want to port your SQL code elsewhere.
May 30 '17 #6
Petrol
204 128KB
Hmm, that's an interesting solution. The query I ran was just for one-off use to populate the historical Walks records with the numbers of people, and Phil's solution worked, so I won't need to run it again. However I'm always interested to learn new approaches, so thanks for the idea. I don't quite understand how it works, though: If the SET statement only increments NoPpl when NoPpl is zero, how can it ever get beyond 1?
Also, why are domain aggregate functions not recommended for use within SQL?
Thanks.
May 30 '17 #7
NeoPa
32,556 Expert Mod 16PB
Petrol:
If the SET statement only increments NoPpl when NoPpl is zero, how can it ever get beyond 1?
It checks [NoPpl] at the start of the UPDATE query. Not every time a record is processed. It works.
Petrol:
Why are domain aggregate functions not recommended for use within SQL?
Because each Domain Aggregate function call has to initialise and de-initialise a recordset. This overhead can add up to a great deal of extra processing (and time of course).

In essence this can be trivial, but isn't a good habit (or way of thinking) to get into, as it can turn out to be important in some datasets. Smaller datasets can be processed without the extra overhead being noticeable, but larger ones can be hard to miss.
May 30 '17 #8
Petrol
204 128KB
Ah, thanks for the explanation.
May 31 '17 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Larry Rekow | last post by:
As part of a macro, I'm trying to automate appending a table with new records. let's say the table 2 has some new records in it, but also has a lot of identical records to table 1. I would...
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
4
by: RustyR | last post by:
I can read/write etc from Access using the standard ADO connections. When I fetch data from tables based on input data in a web form, all is well. I usually create the query in Access, then copy,...
2
by: kevinjbowman | last post by:
I am by no means a SQl Jedi as will be apparent by my question, but I can usually figure out a select statement on my own. I have one today though that really has me stumped. I am working in...
1
by: mrkselm | last post by:
Hi, I am stuck with a problem in MS Access which does not occur in SQL Server and I have been banging my head against the wall for a couple of days now trying to resolve it. Namely, when I...
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
1
by: Etayki | last post by:
Hi! I am trying to find the number of records stored in my table within a MS SQL database. This is part of a Console Application. Here is my code: Sub RecordCount() Dim cmd2 As New...
1
by: alritedonthaveacow | last post by:
hi i have an access query that draws data from a products table and a stock table. i have a count on productid field from the prodcucts table. the problem is that if there is no records in the...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
2
by: nspader | last post by:
OK, I am pulling my hair out on this one. It seems like it should be so simple and I cannot seem to find the answer. I am using Access 2000 on Windows 2000. I have two tables set up. Table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.