Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2003, 09:24 PM
Authorized User
 
Join Date: Aug 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default generate autonumber...urgent!

hi...

I have a table which contains ReceiptNo(Number),ICNo(Number), StickerNo(Autonumber),PlateNo(text) and Date.

Can anyone please tell me how to generate an autonumber back to 1 for each beginning of the month.
 
Old August 22nd, 2003, 06:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Quote:
quote:Originally posted by semooth
Can anyone please tell me how to generate an autonumber back to 1 for each beginning of the month.
Actually, you would never do that. You see, an autonumber's purpose is simply to identify the record and match it up with other records pertaining to it contained in other tables. It would be the key field in the table.

If you have a field that needs to be reset to 1 each month, then this field would not be an autonumber field. It would be simply a case number or transaction number, etc. You would increment this number by one for each added record, but the other field with the autonumber would just keep going higher and higher.

The case number or transaction number (the one that's reset to 1 each month) would be visible to the user, but the autonumber (that's never reset) is never seen by anyone. Again, it's simply used to tie data from one record in one table to another record (or records) in another table.

Example Table:
Code:
Day of Month   AutoNumber (Rec No)   Transaction No.
1              1                     1
2              2                     2
3              3                     3
1              4                     1
2              5                     2
3              6                     3
1              7                     1
2              8                     2
etc.
The autonumber is simply used to match Record 7, for example, to Record 7 in another table. This number will never be seen by anyone. The computer uses it, not the user. The only number you care about the one you reset every month (transaction number).

That's why Microsoft Access made the autonumber "un-resetable."



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old August 23rd, 2003, 09:21 AM
Authorized User
 
Join Date: Jun 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Greg is 100% correct in stating that autonumber fields (and indeed record numbering) in Access is just to identify a record. But there are ways to reset the autonumber field -- I just wouldn't recommend it...

Anyway look at:

http://www.mvps.org/access/general/gen0006.htm for reset info
http://www.mvps.org/access/general/gen0025.htm for autonumber usage
http://www.mvps.org/access/tables/tbl0005.htm to start autonumber from something other than 1
http://www.mvps.org/access/tables/tbl0016.htm for creating an autonumber field from code.

If you use the reset info above just delete the field and then use the last example to add the autonumber field via code to your table. You could even set this up in code with such simple logic as ...

Private Sub MyForm_Load

   Dim dtmDate as Date

   dtmDate = Date()

   FirstOfMonth(dtmDate)

End Sub

Function FirstOfMonth(dtmDate As Date)
' Return a date that is the first day of the month of the date passed
Dim D As Integer, M As Integer, Y As Integer

    If IsNull(dtmDate) Then
        FirstOfMonth = Null
    Else
        D = Day(dtmDate)
        M = Month(dtmDate)
        Y = Year(dtmDate)
        FirstOfMonth = DateSerial(Y, M, 1)
    End If
End Function

If the date equals the First day of the month then your logic can run the code to delete the autonumber field and then readd it to your table - now there is automation!!

Hope this helps you....

Kenny Alligood
 
Old June 8th, 2005, 01:51 PM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's an alternate way of calculating the beginning and ending day of the month:

Public Function BegOfMonth(DateIn As Date) As Date
BegOfMonth = DateAdd("d", -(Day(DateIn) - 1), DateIn)
End Function
Public Function EndofMonth(DateIn As Date) As Date
EndofMonth = DateAdd("d", -1, (DateAdd("m", 1, BegOfMonth(DateIn))))
End Function

[email protected]

 
Old June 8th, 2005, 02:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

The beginning day of any month is simply 1, i.e. DateSerial(YYYY, M, 1). The end date is DateSerial(YYYY, M+1, 0), where YYYY is the year and M is the month of interest. So if you want to know the last day of February this year, it's DateSerial(2005, 3, 0) = February 28, 2005.



Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division





Similar Threads
Thread Thread Starter Forum Replies Last Post
Autonumber problem mankoti_mankoti2000 Access 6 August 29th, 2006 12:28 AM
Autonumber Query Brendan Bartley Access 2 July 16th, 2006 02:14 AM
AutoNumber Assignments jackDaniels SQL Server 2000 10 February 10th, 2005 12:32 AM
regarding Autonumber... MuthuAL Classic ASP Databases 2 December 8th, 2004 08:04 AM
Retrieving the AutoNumber stu9820 ASP.NET 1.0 and 1.1 Basics 11 August 18th, 2004 04:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.