Question : Count specific records and place the sum value in a field

I have an Access 2007 database that I am designing for students to log into when they come into our office.  I want it to automatically track how many times they log in, so we know how often they have visited the office.  There is a table in the database with these fields:
ID
OfficeVisits (which is the # of times they  have visited)
Visit_Date
Visit_Time
ReasonForVisit
EmployeeBeingVisited

When someone logs in, it will automatically add a new record to this database for that visit, so a student who logs in on 4 different occasions would have 4 records in this table.  The question is, how can I tell Access to determine if the current visit is number 2 or 3 or 4, etc.?

One idea I had was to count the number of records in the table with the same ID #, then add 1 to that number and insert it into the OfficeVisits field of the new record. Is that the best way, and if so, how do I count records in VBA?

Answer : Count specific records and place the sum value in a field

select count(*) as visit_count from visits where id=@MyID

where @MyID is your id

select count(*) as visit_count from visits where id='MyID'

before inserting get this count and run your query

or do something like this while inserting

insert into visits(...) values(id, (select count(*) as visit_count from visits where id='MyID'), ...)
Random Solutions  
 
programming4us programming4us