473,386 Members | 1,606 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,386 software developers and data experts.

How to reset sequence number to 1 when new date occurs?

180 100+
I have a form that I use to autogenerate a new projectid that has the format of: currentdate which is yyyymmdd & sequencenum. Example: 20081024-1

Below is the code I have on the current event of the form:

Private Sub Form_Current()
If Me.NewRecord Then
Me.project_sequencenum = DMax("project_sequencenum", "tblprojects") + 1
End If
Me.txtdatesequence = Me.project_date & "-" & Me.project_sequencenum
Me.txtdatesequence = Format(Me.project_date, "yyyymmdd") & "-" & Me.project_sequencenum

End Sub

The problem I am having is that the sequencenum automatically increments, but does not reset to 1 for the first projectid I create on a new date.
How do I have the sequencenum reset to 1 when a new date occurs so that the first projectid created starts with the sequencenum of 1?



Thanks,
Aug 5 '10 #1
5 4430
Steven Kogan
107 Expert 100+
The DMax function would need to specify the date or whatever criteria on which you want a new sequence. It returns null if there is no match, so use the nz function to assign 0 if it is null.

You'd want something like:
Expand|Select|Wrap|Line Numbers
  1. nz(DMax("project_sequencenum", "tblprojects","project_date="& format(me.project_date,"dd-mmm-yyyy")),0)+1
(I'm a bit clumsy with the date format, but this should work...)
Aug 5 '10 #2
Steven Kogan
107 Expert 100+
By the way, as NeoPa pointed out, consider using the Before Insert event to generate the new key field. That way if someone else is adding a record at roughly the same time you are much less likely to get the same number.

Be sure the field is set as a primary key or indexed with no duplicates. That way if a duplicate sequence number is generated for the same day you will be prompted to save again. When you do, the before insert event would fire.
Aug 5 '10 #3
eneyardi
180 100+
it didn't answer my question how to reset to 001 when new date occurs? anyone has a sample program sequence no. reset to 001 when new date occurs?
Aug 24 '10 #4
colintis
255 100+
It looks to me the code you have is just getting the highest seq no. in the table, so it never resets to 1, and it will keeps incrementing.

So you should setup a IF statement in front to check whether the newest projectid in the table is equal to the current date, if yes then you simply set Me.project_sequencenum as 1, otherwise continue the original if statement you have in your own.
Aug 24 '10 #5
Steven Kogan
107 Expert 100+
The code looks for the highest sequence number for a given date and adds one to it. If there are no projects for that date the DMax function returns null, the nz function returns 0, and 1 is added to it, thus restarting the sequence at 1.

This would not work if project_sequencenum is an autonumber field - instead the function is used to generate the number for you.

What is it about the code that doesn't work for you?

Expand|Select|Wrap|Line Numbers
  1. nz(DMax("project_sequencenum", "tblprojects","project_date="& format(me.project_date,"dd-mmm-yyyy")),0)+1 
  2.  
Aug 25 '10 #6

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

Similar topics

1
by: Ken | last post by:
I got an Access database that need to be converted to Oracle 9i. Somehow the Trigger we created to simulate the "AUTO NUMBER" on Access could not create the sequence number as soon as the value has...
0
by: Kamran K | last post by:
Hello I have created a client server application using C#. Existing application is using random number on client side to generate sequence numbers that are then assigned to transactions. This...
12
by: tojigneshshah | last post by:
Hi, I have a situation where i have multiple batch and the each batch are sequence numbers. For each batch, the number should start with 1. For example: Col.no1 Col.no2 ------ ...
5
by: tojigneshshah | last post by:
Hi, How to restart sequence number once the data is imported/loaded into the tables? Please advise. regards Jignesh
1
by: ddice | last post by:
I need to update and then read a sequence number, but I think its failing when there are many users updating at the same time. Here's what I'm doing update datatable set nextnr = nextnr+1 where...
0
by: Mikelowe | last post by:
Would like to know is there an automated way to copy the maxvalue of sequence number from db2 production to db2 test. When we copy data from our production that uses a sequence number value as its...
21
by: bilgekhan | last post by:
After doing a succcessful insert() or find() on a set<Tcontainer is it possible to get the item number of this item in the set? (ie. its zero-based sequence number (position/location/rank/index)...
1
by: kama | last post by:
I want to create sequence number for repeating data values. This sequence number will re-start from 1 for each new value. Example as below:- Amount sequence_number 200 1 200 2...
2
by: XML fellow | last post by:
hi, i would like to define a unique key in the XSD, and once generating a default XML out of it (using XSD2INST tool), the XML will automaticaly be populated with a unique sequence number. ...
12
by: Coni | last post by:
Hi All, I am working in Access to print Invoices. On each invoice there is a line number column which is a field to number each line for a product purchase. I would like to know if there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.