Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create grouped counter column in query

Status
Not open for further replies.

Gerard1

Programmer
Mar 9, 2001
9
0
0
US
I would like to create a counter column in a table/query that resets to 1 upon a change in a particular field.
For example:
PersonID Counter
100 1
100 2
100 3
200 1
200 2
300 1
400 1

Thanks in advance for help!
 
Hi,

Using a form and in the control that represents the PersonID field you enter code into its AFTER_UPDATE event.

Private Sub PersonID_AfterUpdate()
Me!Counter = 1
End Sub

Have a good one!
BK
 
Thanks,
but is there a way to create a table or query with all these values without using forms or reports?
Thanks again!!
:)
 
This is possible using a query if you have either an autonumber type index for the table or a date field

using index
select tbl.personid,(select count(a.personid) from tbl as a where a.personid = tbl.personid and a.index <= tbl.index)as counter
from tbl

using date
select tbl.personid,(select count(a.personid) from tbl as a where a.personid = tbl.personid and a.dtfld <= tbl.dtfld)as counter
from tbl

or you could create a function that would do it.


 
I tried the 1st method and it works.
Thanks a million!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top