473,385 Members | 1,727 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.

Update Query Counter

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.

14 1610
Seth Schrock
2,965 Expert 2GB
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
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, 150 views)
Mar 14 '14 #3
Seth Schrock
2,965 Expert 2GB
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
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
2,965 Expert 2GB
Oops. You need to remove the opening parenthesis on line 2. Thats what happens when you write air code :)
Mar 14 '14 #6
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
2,965 Expert 2GB
Just put a +1 after the ending parenthesis of the DCount, before the As Suvey_Count.
Mar 14 '14 #8
zmbd
5,501 Expert Mod 4TB
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
2,965 Expert 2GB
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
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
2,965 Expert 2GB
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
5,501 Expert Mod 4TB
<<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
GKJR
108 64KB
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
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

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

Similar topics

6
by: David Shorthouse | last post by:
Hello folks, I have a problem with an update query on an asp not updating the table in an Access db. The code runs, I have no errors, but when I examine the table, nothing was updated. The query...
3
by: Bill Clark | last post by:
I have about 20,000 records pulled from Excel that I need to update. What I need to do is run an update query that bascially says: If a field is null, update it with the previous record value of...
5
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the...
10
by: Randy Harris | last post by:
I imported records into a table, later found out that many of them had trailing spaces in one of the fields. If I'd caught it sooner, I could have trimmed the spaces before the import. This...
5
by: Andrew | last post by:
I've got a list box that selects a record on a subform, and in that subform are a few text fiels and a button that runs an update query. How do I have the update query button run and only update...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
7
by: Mark Carlyle via AccessMonster.com | last post by:
I have this update query that I am trying to run. I know the syntax is messed up but do not know how to correct it. Select 'UPDATE', Transactions,'Set = where = ' From "Get Daily Balances" ...
2
by: bobabooey2k | last post by:
I have an update query with one field having in its "Update to" cell a DLookup statement. This query takes 2-3 minutes on 3000 records. Can I avoid dlookup here using multiple queries? An...
4
by: Ian Davies | last post by:
Hello I have seen some tutorials to put a update a counter field in a record. I have the counter field in a table that also has a field for a path to file. I display the records in a table on...
1
by: giovannino | last post by:
Dear all, I did a query which update a sequence number (column NR_SEQUENZA) in a table using a nice code (from Trevor !). 1) Given that I'm not a programmer I can't understand why...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.