Adding numbers automatically in a sequence

oni1983

New Member
Joined
Jul 9, 2015
Messages
25
I have a database which has a number of entries, each entry having a start date and end date. I would like to find a way to assign numbers 1,2 and 3 depending on the start dates of each entry. This needs to be done so that I can then choose all numbered 1 and group them together in a report.

Would it also be possible to automatically give new entries the next consecutive number whilst inputting?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
but add a GroupNumber field to your table and then create update queries (one for each grouping) similar to this one:

UPDATE MyTable SET MyTable.GroupNumber = 1 WHERE MyTable.StartDate Between #1/1/2016# And #1/10/2016#

UPDATE MyTable SET MyTable.GroupNumber = 2 WHERE MyTable.StartDate Between #1/11/2016# And #1/23/2016#

etc.

I am not quite sure if the above is what your were looking for


For consecutive numbering ... you can do this as an AfterInsert event of your form, run the code similar to the following

Code:
Private Sub Form_AfterInsert()

[INDENT]Dim db as DAO.Database
Dim rs as DAO.Recordset
Dim NextNum as Long

Set db = CurrentDB()

' Get the highest stored number and add 1
Set rs = db.OpenRecordSet("SELECT Max([GroupNumber]) FROM MyTable")
NextNum = rs(0) + 1
rs.close
Set rs = Nothing

' Use the form's RecordsetClone so as not to disturb the user interface
Set rs = Me.RecordsetClone

' Move to the last record added
rs.Bookmark = Me.Recordset.LastModified

' Running SQL here instead of doing an Edit, allows other users to see the change instantly
' when running a shared program.  This way you don't accidentally have 2 users end up with
' the same NextNum due to network lag.

db.Excecute "UPDATE MyTable Set GroupNumber = " & NextNum & " WHERE [IDField] = " & rs("IDField"), 
dbFailOnError
Set db = Nothing[/INDENT]

End Sub
Hope this Helps

Art
MS Certified Access Developer
25+ years experience
 
Upvote 0

Forum statistics

Threads
1,221,821
Messages
6,162,157
Members
451,750
Latest member
pnkundalia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top