467,166 Members | 1,070 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

Custom made Autonumber to show today's date

Hi,
I am designing a database for work and would like my autonumber to be of the following format:
yyyymmdd-001
yyyymmdd-002
yyyymmdd-003
So, the date the entry is made(today) dash an increasing number starting at 001 each new day.

Can someone tell me how that's done, or if it's even doable?

Cheers,
Hilde.
Aug 20 '07 #1
  • viewed: 13586
Share:
10 Replies
missinglinq
Expert 2GB
The concept you're talking about is an incremental ID number. Don't call it an Autonumber. An Autonumber is a specific datatype in Access, it's a Long Integer and it's automatically generated by Access.

Here's the link for a Micro$oft tutorial on doing this very thing

http://office.microsoft.com/en-us/ac...550831033.aspx

The left half of your string can be generated with Format(date,"yyyymmdd"). The tutorial will show you how to generate the incrementing part.

Welcome to TheScripts!

Linq ;0)>
Aug 20 '07 #2
ADezii
Expert 8TB
Hi,
I am designing a database for work and would like my autonumber to be of the following format:
yyyymmdd-001
yyyymmdd-002
yyyymmdd-003
So, the date the entry is made(today) dash an increasing number starting at 001 each new day.

Can someone tell me how that's done, or if it's even doable?

Cheers,
Hilde.
Assumptions:
  1. Your Table Name is tblTest.
  2. Your specially formatted AutoNumber Field is called [ID] and it is the Primary Key.
  3. To produce the next AutoNumber in sequence with your specifications:
    Expand|Select|Wrap|Line Numbers
    1. NextAutoNum = Format$(Now(), "yyyymmdd") & "-" & Format$(Val(Right$(DLast("[ID]", "tblTest"),3))+1, "000")
  4. If the last [ID] value was 20070820-034, the next one will be:
    Expand|Select|Wrap|Line Numbers
    1. 20070820-035
Aug 20 '07 #3
Thank you, I combined both of your ideas and think I got something useful out of it :)
H.
Aug 21 '07 #4
Hi,
as I've never done this before, this is the code I ended up with using a code builder in a form (no autonumber).

Dim strMax As String
strMax = DMax("ID", "Register")
Me!HiddenCtl = Format$(Now(), "yyyymmdd") & "-" & Right(strMax, Len(strMax) - InStr(1, strMax, "-")) + 1

This works - sort of. What happens is that the date changes, but the number after the dash doesn't start at 1 again. So I get:
20070821-1
20070821-2
20070822-3
etc...
How can I get it to start over again????

Cheers
Aug 22 '07 #5
I did the same of "hildemoseby", and I have the same result, but I wonder if I can add characters before of the date and the number and the data appear like this

NSR-20100604-1
NSR-20100604-2

Any Help?

thanx
Jun 4 '10 #6
I did the code

Private Sub Name_AfterUpdate()
Dim strMax As Variant
strMax = DMax("fldcount", "tblcount")
Me!HiddenCtl = "NSR" & Format$(Now(), "yyyymmdd") & "-" & Right(strMax, Len(strMax) - InStr(1, strMax, "-")) + 1

End Sub

and I have the result

NSR20100604-1

but the problem now when the increment value reach "10" all increaments stops like this

NSR20100604-8
NSR20100604-9
NSR20100604-10
NSR20100604-10
NSR20100605-10
Jun 4 '10 #7
Bit
Expand|Select|Wrap|Line Numbers
  1. Me.ID = Format(Now(), "yyyymmdd") & "-" & Format(Val(Right(Nz(DLast("[ID]", "Table1"), 0), 3)) + 1, "000")
3 Weeks Ago #8
NeoPa
Expert Mod 16PB
That assumes the last entry - in the order Access presents you the data from that table - is also the most recent. Essentially you're code suffers from the same limitation that ADezii's does from way back in August 2007. In fact it looks pretty identical - even as far as using Now() instead of Date(). You're better off using DMax().
3 Weeks Ago #9
cactusdata
Expert 128KB
It takes a little more:
Expand|Select|Wrap|Line Numbers
  1. NextID = Format(Date, "yyyymmdd\-") & Right("00" & CStr(Val(Nz(DMax("[ID]", "tblTest", "[ID] Like '" & Format(Date, "yyyymmdd\*") & "'"))) + 1), 3)
3 Weeks Ago #10
NeoPa
Expert Mod 16PB
Yes. Well spotted. I did some work on this type of thing a while ago but lost many of my notes when my PC died earlier in the year :-(
3 Weeks Ago #11

Post your reply

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

Similar topics

13 posts views Thread by Tony Williams | last post: by
5 posts views Thread by ndn_24_7 | last post: by
5 posts views Thread by Apple | last post: by
5 posts views Thread by troy_lee@comcast.net | last post: by
6 posts views Thread by =?Utf-8?B?QmVu?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.