473,403 Members | 2,338 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,403 software developers and data experts.

Custom Counter

WEG
I want to create a custom counter to act as the primary field in a table but
am having trouble with it.

What I would like is a counter that shows the last two digits of the year, a
hyphen, then a counter that starts at one and goes up by one digit with each
new record. Then, when a new year starts, would reset to one and start
again.

For instance, 07-001, 07-002 and so on until 08-001, 08-002. I've seen all
kinds of great answers here, so I thank you in advance for the great answer
I'm sure you will give me.
Jun 12 '07 #1
4 3319
WEG wrote:
I want to create a custom counter to act as the primary field in a
table but am having trouble with it.

What I would like is a counter that shows the last two digits of the
year, a hyphen, then a counter that starts at one and goes up by one
digit with each new record. Then, when a new year starts, would
reset to one and start again.

For instance, 07-001, 07-002 and so on until 08-001, 08-002. I've
seen all kinds of great answers here, so I thank you in advance for
the great answer I'm sure you will give me.
Better is to have two fields. One that holds the DateTime of record creation
and one that holds an incrementing number. You can then display what you want
with an expression like...

=Format(CreatedOn,"yy\-") & Format(ID, "000")

The CreatedOn field simply needs a default value of Now(). For the ID use
DMax() in the BeforeUpdate event of the form.

If Me.NewRecord Then
Me.ID = Nz(DMax,"ID", "TableName", Year(CreatedOn) = Year(Date())"),0) + 1
End If
--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Jun 12 '07 #2
Rick Brandt wrote:

Missed a double quote below.
If Me.NewRecord Then
Me.ID = Nz(DMax,"ID", "TableName", Year(CreatedOn) = Year(Date())"),0)
+ 1 End If
Should be...

If Me.NewRecord Then
Me.ID = Nz(DMax,"ID", "TableName", "Year(CreatedOn) = Year(Date())"),0) +
1
End If


--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Jun 12 '07 #3
WEG
Thanks, that did the trick.

"Rick Brandt" <ri*********@hotmail.comwrote in message
news:dZ**************@newssvr17.news.prodigy.net.. .
Rick Brandt wrote:

Missed a double quote below.
>If Me.NewRecord Then
Me.ID = Nz(DMax,"ID", "TableName", Year(CreatedOn) = Year(Date())"),0)
+ 1 End If

Should be...

If Me.NewRecord Then
Me.ID = Nz(DMax,"ID", "TableName", "Year(CreatedOn) = Year(Date())"),0)
+ 1
End If


--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com

Jun 13 '07 #4
WEG wrote:
Thanks, that did the trick.
Should be...

If Me.NewRecord Then
Me.ID = Nz(DMax,"ID", "TableName", "Year(CreatedOn) =
Year(Date())"),0) + 1
End If
Okay, now that you have it working I will give you an improvement. I use the
syntax above because it is easier to understand and shorter so line-wrapping
doesn't make too big a mess of it. However; what would be more efficient would
be to make sure you have an index on the CreatedOn field and use this for the
WHERE clause instead of what I gave you before.

"CreatedOn >= DateSerial(Year(Date()), 1, 1) AND CreatedOn <
DateSerial(Year(Date()) + 1, 1, 1)"

The reason is that whenever you apply criteria to an expression it is
inefficient and cannot use an index. The revised syntax above applies the
criteria directly to the field value so an index can be used and efficiency is
much greater. This would be especially important as your table gets larger.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Jun 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: W1ld0ne74 | last post by:
I am implimenting Performance counters into a web application. I use the following code to create the counters during setup: private void SetupPerfCntrs() {...
0
by: Navin Mishra | last post by:
Hi, I'm having problem adding custom performance counter INSTANCE in an ASP.NET web service whose miltiple instances could run hosted by different IIS AppPools on same machine. I can update my...
0
by: Val Mazur (MVP) | last post by:
Hi guys, My application creates custom performance counter and it works fine. Now I am creating multiple instances of the same counter and increment value for each instance. This part work fine...
0
by: Ivan | last post by:
I have the SystemMonitor ActiveX control on a form and I try to register a custom performance counter. I expect that after registering the counter with the OS, the user would be able to add it...
8
by: Lee | last post by:
Hi all, I'm trying to programatically change the background color of lines of text and running into a few difficulties, anyone got any suggestions? my relevant code is as follows: public void...
0
by: supreeth.bhat | last post by:
I created a new Performance Counter Category and added three new counters. Used InstallUtil.exe to create them on remote production server. I can see the custom counter category and counters. ...
0
by: Trevor L. | last post by:
I decide to put a custom Hit Counter on my page (below). Then I won't be reliant on the standard FrontPage one which uses webbots. It is called by <b>Hit Counter: </b><!--#include...
1
by: SachinSachin | last post by:
Hi All, I am implementing a custom gridview control, that emits some javascript to blink a row whenever a new row is added in gridview. The gridview is inside the <asp:updatepanel> for...
2
by: T-Co | last post by:
I have developed a custom plugin for authorizing streams. I fetch the URL from pPresentationCtx on WMS_EVENT_OPEN with WMS_PRESENT_REQUEST_NAME and go from there. My EnablePlugin() has...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.