Re: Number sequence
- From: Mark G <MarkG@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 21 Aug 2007 07:10:01 -0700
Thank you Tina
"tina" wrote:
well, you're welcome, though it sounds like it's not going to be useful to.
you. but you can always learn from reading other people's code, so here it
is:
Public Function GenNextSequence2(ByVal tbl As String, _
ByVal fld As String) As String
Dim rst As DAO.Recordset, strSQL As String
strSQL = "SELECT Max(CLng(Right([" & fld _
& "],Len([" & fld & "])-3))) AS SeqVal " _
& "FROM " & tbl & " WHERE (((Left([" _
& fld & "],2))=Right(Year(Date()),2)))"
Set rst = CurrentDb.OpenRecordset(strSQL, dbReadOnly)
GenNextSequence2 = Right(Year(Date), 2) _
& "-" & Format(Nz(rst("SeqVal"), 0) + 1, "0000")
End Function
as for forcing users to enter four digits, looks like Uprider's code should
work. i'd have thought that an input mask woudl, as well - but we're working
pretty much in the dark here re the tables and forms setup and how the
program runs.
if there's anything else i can do to help you out, don't hesitate to ask. i
pretty much have a standing offer of assistance to anyone in firefighting,
law enforcement, or military.
hth
"Mark G" <MarkG@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:5AD209B1-E931-4127-9768-1029614323EF@xxxxxxxxxxxxxxxx
Thank you Tina for all the work you put into this. I would be curious tosee
the code you wrote. I did a few tests with UpRiders solution and itseamed
to work well as long as the four digit format is used. If it is allowedto
automatically assign the numbers I don't think there will be a problem,but
we have people who insist on entering the run number on their own and onlycorrect
use two or three digits. I tried to use an input mask to force the
format but it didn't work. Would this be because of the custom code? I*number*,
tried it in both the table properties and also the field properties on the
form.
Thanks again Tina for the time you put into it
Mark
"tina" wrote:
yes, the code does use that custom function also. (very good catch!)
okay, the problem with the code is that it is not incrementing a
thensuch as 999. instead, it is incrementing each *character* in the string
"-999" as a text value; so it is looking at "9" and returning a zero for
each "9" value (because no single character number is higher than 9),
orincrementing the dash (-) to the next Ascii character, which is a period
testeddot. that accounts for the return value of "07.000" that i got when i
youthe code.
the suggestion posted by UpRider would solve your immediate problem, i
think. but you're going to run into the same issue every year, unless
functionhave less than 1000 fire runs in a year. i can post an alternate
eachthat will automatically increment the number, from 1 up to 9999 (i'm
guessing you'll never have that many runs in one year!). i wrote the
function to automatically restart the increment on the first record of
Jannew year - based on the assumption that if you log a run at 12:01 AM on
you1, you will want that run to count as the first run of the new year. if
issuehave lag time between the run and the logging of it, that may be an
storedfor you.
here are some other issues you have to consider: whether you use my
solution or Uprider's, you're going to have problems with sorting the
fieldvalues in the table. because the value is Text, you won't get the right
sequence from *this year's* records. instead, an ascending sort on the
isn'twill return
001
002
....
099
100
1000
1001
....
1009
101
1010
1011
....
1019
102
1020
1021
....
1029
103
1030
1031
....
1039
104
etc, etc.
you can address this issue by using an Update query to change all the
existing RUN# field values for 2007 to match the "-0000" format. this
originalhard to do, but you'll want to make a COPY of the database as a backup
FIRST, so if you hose the data you can start over with a new copy of the
backup.
another issue with my solution is that we don't know where else the
theGenNextSequence() function may be called. if it is called elsewhere in
helpdatabase, you may end up with conflicting data at some point. it's
impossible to say for sure without seeing the database itself.
if you want to see the alternate function i wrote, and/or if you want
performed.}with writing an Update query, or have any other questions, post back
hth
"Mark G" <MarkG@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:4E333217-EF4C-48CC-A39F-D43EBE796352@xxxxxxxxxxxxxxxx
It looks like it also uses "incrementdigit" Here is that one as well.carry,
Function IncrementDigit(c As Byte) As Byte
'{increments a digit in a sequence field. Returns true if there is no
false if another
' carry operation on the next most significant digit must be
for' begin
If (Chr(c) <> "9") And (UCase(Chr(c)) <> "Z") Then
IncrementDigit = c + 1
Else
If c = Asc("9") Then IncrementDigit = Asc("0")
If c = Asc("z") Then IncrementDigit = Asc("a")
If c = Asc("Z") Then IncrementDigit = Asc("A")
End If
End Function
Thanks
Mark
"tina" wrote:
well, i see that the function is using another custom function,
"AdvanceSequence()". since i don't see anything that would account
functionthe
result you described (07.0000), i think we'll need to see that
one,procedure too. find the function, the same way you found the first
theand
post the code, please.
hth
"Mark G" <MarkG@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:49EAB3D6-7744-4531-B6A9-EA910429D52B@xxxxxxxxxxxxxxxx
Thank you for the quick reply, and the instructions. Here is theinformation.
Public Function GenNextSequence(TableName, FieldName, Seed)
' gets next sequence value from TableName and FieldName, Seed is
"]starting value
Dim rs
On Error Resume Next
Seed = Nz(Seed, "0")
Set rs = CurrentDb.OpenRecordset("Select TOP 1 [" & FieldName &
DESC")as
SeqVal from [" & TableName & "] ORDER BY [" & FieldName & "]
Seed,If Not rs.EOF Then Seed = CStr(IIf(Nz(rs("SeqVal"), "") = "",
thisrs("SeqVal")))
GenNextSequence = AdvanceSequence(CStr(Seed))
If Err Then GenNextSequence = "#SeqErr"
rs.Close
Set rs = Nothing
End Function
Thanks
Mark
"tina" wrote:
looks like a custom function: GenNextSequence(). assuming that
tois
where the number assignment is actually coming from, we'd need
thesee
the
function procedure in order to make recommendations.
open your database, and open any module on the Modules tab. from
choosemenu
GenNextSequence("bar, click Edit | Find. in the Find dialog, type "Function
only without the double quotes. in the Search section below,
lineCurrent
Project, then click Find Next.
you should find the function procedure, which starts with the
end,you
Function" -typed
in (Function may be preceded by Public), and ends with "End
again, sans quotes. copy the entire procedure from beginning to
haveand
automaticallypaste it into a post, so we can see it.
hth
"Mark G" <MarkG@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:585481AD-82D6-44E9-85B4-BE8570063882@xxxxxxxxxxxxxxxx
We have a field in one of our forms in our database that
that wegenerates a run number for fire calls in sequence. The format
use
is
07-999. Everything was working fine until we hit what should
thebeen
07-1000. Now when it is supposed to assign the next number in
wejustsequence
it displays 07.0000. Any help would be greatly apreciated. I
rest ofdabble
with access and have no formal training which is more than the
the
department, and since I do I kind of inherited this database
toare
thinkusing.
This code is in a tab labeled "calculations" on a subform. I
this
is
what is telling the field what to do.
=GenNextSequence("daylog3","RUN#","05-0001"). I would like it
numbers.transition
from 07-999 to 07-1000 just as it did for the first 999
Thanks
Mark
- References:
- Re: Number sequence
- From: tina
- Re: Number sequence
- From: Mark G
- Re: Number sequence
- From: tina
- Re: Number sequence
- From: Mark G
- Re: Number sequence
- From: tina
- Re: Number sequence
- From: Mark G
- Re: Number sequence
- From: tina
- Re: Number sequence
- Prev by Date: Re: Random Number not so random
- Next by Date: Re: How can I use a command button to back my database in Access 2007 ?
- Previous by thread: Re: Number sequence
- Next by thread: Multiple columns
- Index(es):