Customized Autonumbering

G

Guest

I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August 23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

Assuming you are using the current date and time, set the Default Value
property of the field to:
=Format(Now(),"yyddhnn")

Steve
 
J

John Spencer

This expression will give you the base value
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Now(),"nn")

Then you would need to check for an existing value and increment as needed.

Function getReviewID() as string
Dim strReturn as String
Dim lngCount as Long

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(#10:01#,"nn")
lngCount = 0
While DCount("*","YourTable","ReviewId=""" & strReturn & """") > 0 And
LngCount < 100
strReturn = Left(StrReturn,5) & Format(Val(Right(strReturn,2)) +
1,"00")
lngCount = lngCount +1
Wend

If lngCount <100 then
getReviewID = strReturn
else
getReviewID = ""
End If

End Function

There is the problem that you can only have 100 records entered on any one
date

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
G

Guest

There is the problem that you can only have 100 records entered on any one

Highly unlikely. The minute vaule can recur only 24 times in a give day.

I believe the DCount function would be a bit on the slow side. I would
respectfully suggest a DLookup instead.

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

Do While True
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
Me.Return & "'") Then
strReturn = Replace(strReturn,right(strReturn,2),
format(clng(right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End IF
Loop

--
Dave Hargis, Microsoft Access MVP


John Spencer said:
This expression will give you the base value
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Now(),"nn")

Then you would need to check for an existing value and increment as needed.

Function getReviewID() as string
Dim strReturn as String
Dim lngCount as Long

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(#10:01#,"nn")
lngCount = 0
While DCount("*","YourTable","ReviewId=""" & strReturn & """") > 0 And
LngCount < 100
strReturn = Left(StrReturn,5) & Format(Val(Right(strReturn,2)) +
1,"00")
lngCount = lngCount +1
Wend

If lngCount <100 then
getReviewID = strReturn
else
getReviewID = ""
End If

End Function

There is the problem that you can only have 100 records entered on any one
date

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Don said:
I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August
23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at
all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a
duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

Steve, thanks for an easily understandable answer. There are only two
problems with the result that I get. First, it doesn't place the correct
julian date. My computer is set to the correct date, but the julian that is
being calculated is off by five days. Is there somewhere else in the system
that is driving this date?
Second, it is not accounting for possible duplicates and incrementing until
a non-duplicate is found. With the way you have it, there should not be a
problem as we would be lucky to add 5 new records per day, but in that
outside chance, what would you recommend? The other thread postings appear to
address the issue but I don't know where to place that code.
--
Thanks!


SteveM said:
Assuming you are using the current date and time, set the Default Value
property of the field to:
=Format(Now(),"yyddhnn")

Steve

Don said:
I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August 23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

Where would I be placing this code?
--
Thanks!


Klatuu said:
There is the problem that you can only have 100 records entered on any one
date

Highly unlikely. The minute vaule can recur only 24 times in a give day.

I believe the DCount function would be a bit on the slow side. I would
respectfully suggest a DLookup instead.

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

Do While True
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
Me.Return & "'") Then
strReturn = Replace(strReturn,right(strReturn,2),
format(clng(right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End IF
Loop

--
Dave Hargis, Microsoft Access MVP


John Spencer said:
This expression will give you the base value
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Now(),"nn")

Then you would need to check for an existing value and increment as needed.

Function getReviewID() as string
Dim strReturn as String
Dim lngCount as Long

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(#10:01#,"nn")
lngCount = 0
While DCount("*","YourTable","ReviewId=""" & strReturn & """") > 0 And
LngCount < 100
strReturn = Left(StrReturn,5) & Format(Val(Right(strReturn,2)) +
1,"00")
lngCount = lngCount +1
Wend

If lngCount <100 then
getReviewID = strReturn
else
getReviewID = ""
End If

End Function

There is the problem that you can only have 100 records entered on any one
date

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Don said:
I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August
23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at
all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a
duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

John,
The first part of your response works perfectly, but I don't know where to
place the code for the second part of your response for the incrementation.
Please advise.
--
Thanks!


John Spencer said:
This expression will give you the base value
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Now(),"nn")

Then you would need to check for an existing value and increment as needed.

Function getReviewID() as string
Dim strReturn as String
Dim lngCount as Long

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(#10:01#,"nn")
lngCount = 0
While DCount("*","YourTable","ReviewId=""" & strReturn & """") > 0 And
LngCount < 100
strReturn = Left(StrReturn,5) & Format(Val(Right(strReturn,2)) +
1,"00")
lngCount = lngCount +1
Wend

If lngCount <100 then
getReviewID = strReturn
else
getReviewID = ""
End If

End Function

There is the problem that you can only have 100 records entered on any one
date

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Don said:
I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August
23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at
all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a
duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

=Format(Now(),"yyddhnn")
Is not returning the 3 digit day. It is returning a two digit year, a two
digit day which is the current day of the month, not the current number of
the day in the year, a 1 digit hour and a two digit minute. The day appears
to be off by five just cooincidently, because it is the 24th. The h is
actually return 2 digits because the hour is past 9.

The correct formula is:
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) & Format(Time,"nn")
Which as I am writing this, is:
0723600

It was exactly 12:00 when I tested this.

Now, the incrementing may not be simple enough to understand at first
glance. I made a suggestion on a modification to John Spencer's repsonse,
but let me put it all together for you:

Private Sub GetNextNum()
Dim strReturn As String

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

Do While True
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
strReturn & "'") Then
strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End IF
Loop
End Sub

Now, let's break it down:

'Format the YYJJJMM (Year, Julian Date, Minute) as a string

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

'Creates an endless loop True is always True (except when you watch CBS) <g>
Do While True

'Do a DLookup in the table for an existing duplicate value. If the value is
not in the table, Null will return. If you get a return, it is a duplicate
and you need to increment the number
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
strReturn & "'") Then

'Duplicate Number, increment
'Convert the last 2 characters of the string to a number
'Add 1 to the number
'Convert it back to a string
'Replace the last two digts of the current string with the incremented value
string
'Note this is different from my original post, but there is a bug in the other
strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else

'We got a Null returned which means the value does not exist in the table
'Populate the control with the new value
Me.strReviewID = strReturn
'Jump out of the loop
Exit Do
End IF
Loop

If you need more help, post back

--
Dave Hargis, Microsoft Access MVP


Don said:
Steve, thanks for an easily understandable answer. There are only two
problems with the result that I get. First, it doesn't place the correct
julian date. My computer is set to the correct date, but the julian that is
being calculated is off by five days. Is there somewhere else in the system
that is driving this date?
Second, it is not accounting for possible duplicates and incrementing until
a non-duplicate is found. With the way you have it, there should not be a
problem as we would be lucky to add 5 new records per day, but in that
outside chance, what would you recommend? The other thread postings appear to
address the issue but I don't know where to place that code.
--
Thanks!


SteveM said:
Assuming you are using the current date and time, set the Default Value
property of the field to:
=Format(Now(),"yyddhnn")

Steve

Don said:
I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August 23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

KLatuu,
I have done as instructed (I think) by placing this code as part of an event
in my form as a GotFocus event, however, when I try to open the form I am
immediately sent to debug with a compile syntax error at "then". Do I want
this on the form or in the table? If this code goes in the table, where do I
put it on the strReviewID control?

Private Sub strReviewID_GotFocus()
Dim strReturn As String

strReturn = Format(Date, "yy") & Format(DatePart("y", Date, "000")) &
Format(Time, "nn")

Do While True
If Not IsNull(DLookup("[strReviewID]", "MyTable", "[strReviewID] =
'" & strReturn & "'") Then strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End If

Me.strReviewID = strReturn
Exit Do

Loop
End Sub
--
Thanks!


Klatuu said:
=Format(Now(),"yyddhnn")
Is not returning the 3 digit day. It is returning a two digit year, a two
digit day which is the current day of the month, not the current number of
the day in the year, a 1 digit hour and a two digit minute. The day appears
to be off by five just cooincidently, because it is the 24th. The h is
actually return 2 digits because the hour is past 9.

The correct formula is:
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) & Format(Time,"nn")
Which as I am writing this, is:
0723600

It was exactly 12:00 when I tested this.

Now, the incrementing may not be simple enough to understand at first
glance. I made a suggestion on a modification to John Spencer's repsonse,
but let me put it all together for you:

Private Sub GetNextNum()
Dim strReturn As String

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

Do While True
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
strReturn & "'") Then
strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End IF
Loop
End Sub

Now, let's break it down:

'Format the YYJJJMM (Year, Julian Date, Minute) as a string

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

'Creates an endless loop True is always True (except when you watch CBS) <g>
Do While True

'Do a DLookup in the table for an existing duplicate value. If the value is
not in the table, Null will return. If you get a return, it is a duplicate
and you need to increment the number
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
strReturn & "'") Then

'Duplicate Number, increment
'Convert the last 2 characters of the string to a number
'Add 1 to the number
'Convert it back to a string
'Replace the last two digts of the current string with the incremented value
string
'Note this is different from my original post, but there is a bug in the other
strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else

'We got a Null returned which means the value does not exist in the table
'Populate the control with the new value
Me.strReviewID = strReturn
'Jump out of the loop
Exit Do
End IF
Loop

If you need more help, post back

--
Dave Hargis, Microsoft Access MVP


Don said:
Steve, thanks for an easily understandable answer. There are only two
problems with the result that I get. First, it doesn't place the correct
julian date. My computer is set to the correct date, but the julian that is
being calculated is off by five days. Is there somewhere else in the system
that is driving this date?
Second, it is not accounting for possible duplicates and incrementing until
a non-duplicate is found. With the way you have it, there should not be a
problem as we would be lucky to add 5 new records per day, but in that
outside chance, what would you recommend? The other thread postings appear to
address the issue but I don't know where to place that code.
--
Thanks!


SteveM said:
Assuming you are using the current date and time, set the Default Value
property of the field to:
=Format(Now(),"yyddhnn")

Steve

:

I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August 23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

The code does not go in a table. I doubt it goes in the got focus event. If
it is for creating new records, it most likely needs to be in the Form
Current event:

If Me.NewRecord Then
Call GetNextNum
End if
--
Dave Hargis, Microsoft Access MVP


Don said:
KLatuu,
I have done as instructed (I think) by placing this code as part of an event
in my form as a GotFocus event, however, when I try to open the form I am
immediately sent to debug with a compile syntax error at "then". Do I want
this on the form or in the table? If this code goes in the table, where do I
put it on the strReviewID control?

Private Sub strReviewID_GotFocus()
Dim strReturn As String

strReturn = Format(Date, "yy") & Format(DatePart("y", Date, "000")) &
Format(Time, "nn")

Do While True
If Not IsNull(DLookup("[strReviewID]", "MyTable", "[strReviewID] =
'" & strReturn & "'") Then strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End If

Me.strReviewID = strReturn
Exit Do

Loop
End Sub
--
Thanks!


Klatuu said:
=Format(Now(),"yyddhnn")
Is not returning the 3 digit day. It is returning a two digit year, a two
digit day which is the current day of the month, not the current number of
the day in the year, a 1 digit hour and a two digit minute. The day appears
to be off by five just cooincidently, because it is the 24th. The h is
actually return 2 digits because the hour is past 9.

The correct formula is:
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) & Format(Time,"nn")
Which as I am writing this, is:
0723600

It was exactly 12:00 when I tested this.

Now, the incrementing may not be simple enough to understand at first
glance. I made a suggestion on a modification to John Spencer's repsonse,
but let me put it all together for you:

Private Sub GetNextNum()
Dim strReturn As String

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

Do While True
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
strReturn & "'") Then
strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else
Me.strReviewID = strReturn
Exit Do
End IF
Loop
End Sub

Now, let's break it down:

'Format the YYJJJMM (Year, Julian Date, Minute) as a string

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Time,"nn")

'Creates an endless loop True is always True (except when you watch CBS) <g>
Do While True

'Do a DLookup in the table for an existing duplicate value. If the value is
not in the table, Null will return. If you get a return, it is a duplicate
and you need to increment the number
If Not IsNull(DLookup("[ReviewID]", "MyTable", "[ReviewID] = '" &
strReturn & "'") Then

'Duplicate Number, increment
'Convert the last 2 characters of the string to a number
'Add 1 to the number
'Convert it back to a string
'Replace the last two digts of the current string with the incremented value
string
'Note this is different from my original post, but there is a bug in the other
strReturn = Left(strReturn,5) &
Format(Clng(Right(strReturn,2))+1,"00"))
Else

'We got a Null returned which means the value does not exist in the table
'Populate the control with the new value
Me.strReviewID = strReturn
'Jump out of the loop
Exit Do
End IF
Loop

If you need more help, post back

--
Dave Hargis, Microsoft Access MVP


Don said:
Steve, thanks for an easily understandable answer. There are only two
problems with the result that I get. First, it doesn't place the correct
julian date. My computer is set to the correct date, but the julian that is
being calculated is off by five days. Is there somewhere else in the system
that is driving this date?
Second, it is not accounting for possible duplicates and incrementing until
a non-duplicate is found. With the way you have it, there should not be a
problem as we would be lucky to add 5 new records per day, but in that
outside chance, what would you recommend? The other thread postings appear to
address the issue but I don't know where to place that code.
--
Thanks!


:

Assuming you are using the current date and time, set the Default Value
property of the field to:
=Format(Now(),"yyddhnn")

Steve

:

I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example, August 23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it at all
possible to have this field automatically number itself when opening a new
record? Also, if for some odd reason the system does calculate a duplicate,
it should increment the number by one until a non duplicate is found.
 
J

John Spencer

I would probably place it in a standard vba module and then call it from the
form's before insert event.

Me.txtReviewID = getReviewID()

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Don said:
John,
The first part of your response works perfectly, but I don't know where to
place the code for the second part of your response for the
incrementation.
Please advise.
--
Thanks!


John Spencer said:
This expression will give you the base value
Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(Now(),"nn")

Then you would need to check for an existing value and increment as
needed.

Function getReviewID() as string
Dim strReturn as String
Dim lngCount as Long

strReturn = Format(Date(),"yy") & Format(DatePart("y",Date(),"000")) &
Format(#10:01#,"nn")
lngCount = 0
While DCount("*","YourTable","ReviewId=""" & strReturn & """") > 0 And
LngCount < 100
strReturn = Left(StrReturn,5) & Format(Val(Right(strReturn,2)) +
1,"00")
lngCount = lngCount +1
Wend

If lngCount <100 then
getReviewID = strReturn
else
getReviewID = ""
End If

End Function

There is the problem that you can only have 100 records entered on any
one
date

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

Don said:
I have a control in an Access 2003 table that is set to text because of
alphanumeric needs. If I change the field to numeric, it breaks too
many
other functions to be worth messing with.

Currently, this control [strReviewID] has a standard format that is
use,
e.g. 2 digit century,3 digit julian date,2 digit minute. [Example,
August
23,
2007 at 6:17 pm would equate to 0723517]. No duplicates allowed. Is it
at
all
possible to have this field automatically number itself when opening a
new
record? Also, if for some odd reason the system does calculate a
duplicate,
it should increment the number by one until a non duplicate is found.
 
G

Guest

Still having problems. Let me explain what I have done thus far to see if I
am understanding your instruction.

I open a form named "frmAddNewReview", which is set to automatically go to a
new record upon open. On this form I have placed an "On Current" event
proceedure as follows:

Private Sub Form_Current()
If Me.NewRecord Then
Call GetNextNum
End If
End Sub

Then within the form I have a control [strReviewID] named "GetNextNum" which
is bound to table "tblReview". On this control, I have placed an "on got
focus" event proceedure as follows:

Private Sub GetNextNum_GotFocus()
Dim strReturn As String

strReturn = Format(Date, "yy") & Format(DatePart("y", Date, "000")) &
Format(Time, "nn")

Do While True
If Not IsNull(DLookup("[strReviewID]", "MyTable", "[strReviewID] ='" &
strReturn & "'")) Then
strReturn = Left(strReturn, 5) & Format(CLng(Right(strReturn, 2)) + 1,
"00")
Else
Me.strReviewID = strReturn
Exit Do
End If

Me.strReviewID = strReturn
Exit Do

Loop
End Sub

With both of these events, my database sends me to debug. What am I doing
wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top