By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,518 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,518 IT Pros & Developers. It's quick & easy.

Update Query Counter

P: 16
I am trying to write an update query in MS Access to update a count field in my database. I have a table that has records with multiple fields for every time a surveyor goes to a particular survey point. The surveyor enters information into the database to populate the fields including the point number([Survey_point]), the date([Date_surveyed]), time, etc. and usually goes to point multiple times in a year (but not always).

I would like to add a field called "Visit_Count" and update the value to indicate which visit it is for a given year. So, if the surveyor visits the point a single time in one year, Visit_Count = 1 for that record. But if the surveyor goes to the point 10 times, the first record for a particular sample point Visit_Count = 1, the second = 2, and so on. I have no problem querying the total visits for each point, but I can't figure how to determine that on date X [Date_surveyed](which is stored in record Y[ID]), it was the surveyor's ?th visit ([Survey_count]).

So to simplify, here is a reduced list fields in the table:
ID
Surveyor_ID
Survey_Point
Date_surveyed
Survey_Count 'need to update this field

I would be grateful if anyone could point me in the right direction! Thanks.
Mar 14 '14 #1

✓ answered by Seth Schrock

You shouldn't store this information in a field as you are supposed to store calculated values. Instead what you can do is create a query that has the VisitCount field added to it as a calculated field. That way, if ever a record gets changed from one surveyor to another, then you don't have to fix your visit counts. This is a bit of a complicated query, and I don't have a test database available at the moment, so there might be some errors, but just let me know what they are and we can work through them.
Expand|Select|Wrap|Line Numbers
  1. SELECT ID
  2. , Surveyor_ID
  3. , Survey_Point
  4. , DateSurveyed
  5. , DCount("*", "Survey_Table"
  6.     , "Surveyor_ID=" & Surveyor_ID & 
  7.     " And Survey_Point=" & Survey_Point & 
  8.     " And Year(Date_Surveyed) =" & Year(Date_Surveyed) & 
  9.     " And ID<" & ID) + 1 As Survey_Count
  10. FROM Survey_Table
All you should have to do is change the name of the table in lines 5 and 10.

Share this Question
Share on Google+
14 Replies


Seth Schrock
Expert 2.5K+
P: 2,951
You shouldn't store this information in a field as you are supposed to store calculated values. Instead what you can do is create a query that has the VisitCount field added to it as a calculated field. That way, if ever a record gets changed from one surveyor to another, then you don't have to fix your visit counts. This is a bit of a complicated query, and I don't have a test database available at the moment, so there might be some errors, but just let me know what they are and we can work through them.
Expand|Select|Wrap|Line Numbers
  1. SELECT ID
  2. , Surveyor_ID
  3. , Survey_Point
  4. , DateSurveyed
  5. , DCount("*", "Survey_Table"
  6.     , "Surveyor_ID=" & Surveyor_ID & 
  7.     " And Survey_Point=" & Survey_Point & 
  8.     " And Year(Date_Surveyed) =" & Year(Date_Surveyed) & 
  9.     " And ID<" & ID) + 1 As Survey_Count
  10. FROM Survey_Table
All you should have to do is change the name of the table in lines 5 and 10.
Mar 14 '14 #2

P: 16
Ok. I think Dcount is the right direction I should go. I've attached a reallllly simplified screen shot version of what the data look like. If I can see an example implemented in this, I think I can apply it to the real thing.

Right now, the problem I am having is that it seems to be returning a full count of all the records for some reason, not just returning an incremented value.
Attached Images
File Type: jpg Capture.jpg (54.5 KB, 131 views)
Mar 14 '14 #3

Seth Schrock
Expert 2.5K+
P: 2,951
I'm assuming that Name_ID is replacing Surveyor_ID from your OP? In that case, line #6 should be
Expand|Select|Wrap|Line Numbers
  1.     ,"Name_id='" & Name_id & "'" &
Mar 14 '14 #4

P: 16
I am getting the error 'Missing operator in query or expression'

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. (ID
  3. , Name_ID
  4. , Survey_Point
  5. , Survey_date
  6. , DCount("*", "tbl_survey_details"
  7.     , "Name_ID='" & Name_ID & "'" &
  8.     " And Survey_Point=" & Survey_Point & 
  9.     " And Year(Survey_date) =" & Year(Survey_date) & 
  10.     " And ID<" & ID) As Survey_Count
  11. FROM tbl_survey_details;
Mar 14 '14 #5

Seth Schrock
Expert 2.5K+
P: 2,951
Oops. You need to remove the opening parenthesis on line 2. Thats what happens when you write air code :)
Mar 14 '14 #6

P: 16
IT WORKED! But it starts counting at zero instead of 1. This isn't a huge deal, but is there a quick fix?

...i should have noticed the open parenthesis...i have been staring at the screen too long
Mar 14 '14 #7

Seth Schrock
Expert 2.5K+
P: 2,951
Just put a +1 after the ending parenthesis of the DCount, before the As Suvey_Count.
Mar 14 '14 #8

zmbd
Expert Mod 5K+
P: 5,397
Actually it sounds like you need to normalize your database.[*]> Database Normalization and Table Structures.

For example a very simplistic normalization:
tbl_employee (has basic emloyee names)
tbl_sites (has basic site information)
tbl_visits (has the basic information for each visit)

tbl_visit would be related to tbl_employee and tbl_sites
[visit_pk] autonumber; primary key
[visit_date] date/time
[visit_fk_employee] 1:M relationship from tbl_employee
[visit_fk_sites] 1:M relationship from tbl_employee
[visit_testvalue1] (just an example field - this might be removed as follows)

The test results (i.e. [visit_testvalue1] ) might be broken out of the tbl_visits such that we have a table to hold the data and a table for each test type...
tbl_testresult
[testresult_pk] autonumber; primary key
[testresult_fk_visit] 1:M relationship from tbl_vist
[testresult_fk_test] 1:M relationship from tbl_test
[testresult_result]

Using table tbl_visit, you could agregate query on the emplyee number and count the sites, or agregate qeuery on sites and see the total number of visits, or any number of other possiblities all without haveing to add another field, counters, logic to keep the counters straight, etc...

Something to consider especially if you are still early in design.
Mar 15 '14 #9

Seth Schrock
Expert 2.5K+
P: 2,951
However, an aggregate query wouldn't give the visit count for each record as required in the OP, second paragraph. It would only give the total number of visits. I'm all for normalizing though.
Mar 15 '14 #10

P: 16
I think normalizing would solve many of my problems I am having, and many of my previous problems, but I don't have that much experience with using normalized dbases and my collaborators like to be able to look at all my tables as if they are looking at a spreadsheet.

Thanks for all your help.
Mar 15 '14 #11

Seth Schrock
Expert 2.5K+
P: 2,951
That is the purpose of queries. In reality, users should never look directly at the tables. You arrange the data the way the user needs it by manipulating it with the query.
Mar 15 '14 #12

zmbd
Expert Mod 5K+
P: 5,397
<<Seth>> However, an aggregate query wouldn't give the visit count for each record as required in the OP, second paragraph. It would only give the total number of visits. I'm all for normalizing though.
@Seth:
Yep, I missed returning the record with the nth visit...
However, with that said... the normalized table tbl_visit, filtered by year, could be sorted on the date/time provided the time was entered... so if the employee visited site 1 on 1/1/2014 08:00; 1/1/2014 09:00; etc... then the records sorted by date and the nth record returned therefrom. Easy enough to do in VBA (^_^) Pure SQL... I haven't thought about that in awhile, sure there's a way... another will post, I'm thinking ranking query


@birdDBheadache
I think normalizing would solve many of my problems I am having, and many of my previous problems, but I don't have that much experience with using normalized dbases
Hence the link to the normalization in my last post (^_^)
If you'd like I can PM you a copy of some basic tutorials and concept links. One is a hands on type that once you work thru it you'll have a good understanding of the basics.

my collaborators like to be able to look at all my tables as if they are looking at a spreadsheet.
Seth is very right in that the normal end user shouldn't be messing with the tables!

Collaborators.... ( grimace )
And, so long as they don't want to edit data (and there are workarounds, messy, but possible) a cross-tab-query can be used to present the information in a flattened/standard spreadsheet format. Allen Browne's •Crosstab query techniques
Mar 15 '14 #13

100+
P: 104
As far as a visit counter, one popular method in Access reports is to create an unbound text box with the property for running sum set to on and the total over group option selected. Also set the default value to 1. You could then show a report of visits that is grouped by location and the text box will increment by one for each consecutive visit. This doesn't work for forms or queries, but like the others posted about users not seeing your tables, I would go a step further and say your collaborators shouldn't see your info in a query either. Why not just put it in a report based on your query?

As far as normalization goes, do yourself a favor and get your tables set up properly now before you get any further. It is only going to get more difficult the longer you wait. Normalized tables are an absolute must for a relational database.
Mar 15 '14 #14

P: 16
I am interested in learning more about using normalized databases, so I appreciate all the information. I have made them in the past, but it has been several years. I think I will see this current project to the end and then rethink the approach. All of the data are entered so, the database won't be getting any larger for another year.

Thanks again!
Mar 15 '14 #15

Post your reply

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