473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Resetting automatic File Number at start of new year

tdw
206 New Member
In my Orders database (which someone else created and I have been improving), when I enter a new order it generates a file number (i.e. 2007-0001) using an autonumber REC_ID field. I'll show you the code that it uses.
The following occurs upon a button click:
Expand|Select|Wrap|Line Numbers
  1. If Me![REC_ID] <> 0 And Me![FILE_NO] = "" Then
  2.     DoCmd.SetWarnings False
  3.         If Me![REC_ID] - 10493 < 10 Then ' Numeric Value 10493 is last value of rec_id upon year change for file_id renum
  4.         DoCmd.OpenQuery "FILE_NO_Update_New_000", acNormal, acEdit
  5.         GoTo Confirm_File_No_Creation
  6.         End If
  7.  
  8.         If Me![REC_ID] - 10493 < 100 Then
  9.         DoCmd.OpenQuery "FILE_NO_Update_New_00", acNormal, acEdit
  10.         GoTo Confirm_File_No_Creation
  11.         End If
  12.  
  13.         If Me![REC_ID] - 10493 < 1000 Then
  14.         DoCmd.OpenQuery "FILE_NO_Update_New_0", acNormal, acEdit
  15.         GoTo Confirm_File_No_Creation
  16.         End If
  17.  
  18.     DoCmd.OpenQuery "FILE_NO_Update_New", acNormal, acEdit
  19.  
Here are the update queries:
Expand|Select|Wrap|Line Numbers
  1. [b](FILE_NO_Update_New_000)[B/]
  2.  
  3. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-000" & [REC_ID]-"10493"
  4. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  5.  
  6. [b](FILE_NO_Update_New_00)[B/]
  7.  
  8. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-00" & [REC_ID]-"10493"
  9. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  10.  
  11. [b](FILE_NO_Update_New_0)[B/]
  12.  
  13. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-0" & [REC_ID]-"10493"
  14. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  15.  
  16. (FILE_NO_Update_New)
  17.  
  18. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-" & [REC_ID]-"10493"
  19. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  20.  
It seems pretty complex to me, but I understand it. The problem is that at the beginning of every year, we reset our file number back to 0001 (i.e. the first order of 2006 was 2006-0001, first order of 2007 was 2007-0001, etc.). The only way we know to accomplish this is to go into the code for the form and all three queries to change the REC_ID number being used in the calculations (the 10493 number in these examples). To know what to change it to, we have to find the highest REC_ID (the last order entered the previous year), which could be in either of two tables (one table of open orders: SC_OPEN, and another table of closed, or archived, orders: SC_ARCH). To do that, we have to manually open each table and look for ourselves. This can be complicated and very easy to badly screw up for non-programmers to try to accomplish, and it's not reasonable to expect that a programmer will always be available first thing in the morning on the first work day of the year to make these changes.

It would seem to me that there is simpler way, maybe even an automated way, to have our file number reset to 0001 at the beginning of each year. Any thoughts? Any suggestions? Should the whole process be reworked?
Apr 24 '07 #1
38 4656
ADezii
8,834 Recognized Expert Expert
In my Orders database (which someone else created and I have been improving), when I enter a new order it generates a file number (i.e. 2007-0001) using an autonumber REC_ID field. I'll show you the code that it uses.
The following occurs upon a button click:
Expand|Select|Wrap|Line Numbers
  1. If Me![REC_ID] <> 0 And Me![FILE_NO] = "" Then
  2.     DoCmd.SetWarnings False
  3.         If Me![REC_ID] - 10493 < 10 Then ' Numeric Value 10493 is last value of rec_id upon year change for file_id renum
  4.         DoCmd.OpenQuery "FILE_NO_Update_New_000", acNormal, acEdit
  5.         GoTo Confirm_File_No_Creation
  6.         End If
  7.  
  8.         If Me![REC_ID] - 10493 < 100 Then
  9.         DoCmd.OpenQuery "FILE_NO_Update_New_00", acNormal, acEdit
  10.         GoTo Confirm_File_No_Creation
  11.         End If
  12.  
  13.         If Me![REC_ID] - 10493 < 1000 Then
  14.         DoCmd.OpenQuery "FILE_NO_Update_New_0", acNormal, acEdit
  15.         GoTo Confirm_File_No_Creation
  16.         End If
  17.  
  18.     DoCmd.OpenQuery "FILE_NO_Update_New", acNormal, acEdit
  19.  
Here are the update queries:
Expand|Select|Wrap|Line Numbers
  1. [b](FILE_NO_Update_New_000)[B/]
  2.  
  3. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-000" & [REC_ID]-"10493"
  4. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  5.  
  6. [b](FILE_NO_Update_New_00)[B/]
  7.  
  8. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-00" & [REC_ID]-"10493"
  9. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  10.  
  11. [b](FILE_NO_Update_New_0)[B/]
  12.  
  13. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-0" & [REC_ID]-"10493"
  14. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  15.  
  16. (FILE_NO_Update_New)
  17.  
  18. UPDATE SC_NEW SET SC_NEW.FILE_NO = Year(Date()) & "-" & [REC_ID]-"10493"
  19. WHERE ((([Forms]![Add New Orders]![REC_ID])=[SC_NEW]![REC_ID]));
  20.  
It seems pretty complex to me, but I understand it. The problem is that at the beginning of every year, we reset our file number back to 0001 (i.e. the first order of 2006 was 2006-0001, first order of 2007 was 2007-0001, etc.). The only way we know to accomplish this is to go into the code for the form and all three queries to change the REC_ID number being used in the calculations (the 10493 number in these examples). To know what to change it to, we have to find the highest REC_ID (the last order entered the previous year), which could be in either of two tables (one table of open orders: SC_OPEN, and another table of closed, or archived, orders: SC_ARCH). To do that, we have to manually open each table and look for ourselves. This can be complicated and very easy to badly screw up for non-programmers to try to accomplish, and it's not reasonable to expect that a programmer will always be available first thing in the morning on the first work day of the year to make these changes.

It would seem to me that there is simpler way, maybe even an automated way, to have our file number reset to 0001 at the beginning of each year. Any thoughts? Any suggestions? Should the whole process be reworked?
we have to find the highest REC_ID (the last order entered the previous year), which could be in either of two tables (one table of open orders: SC_OPEN, and another table of closed, or archived, orders: SC_ARCH).
__1. This process can be autuomated via a Public Function which returns the highest REC_ID in either 1 of 2 Tables and can be used virtually anywhere in the Database:
Expand|Select|Wrap|Line Numbers
  1. Public Function fFindHighestREC_ID() As Long
  2. Dim lngID_1 As Long, lngID_2 As Long
  3.  
  4. ID_1 = DLast("[REC_ID]", "SC_OPEN")
  5. ID_2 = DLast("[REC_ID]", "SC_ARCH")
  6.  
  7. If ID_1 > ID_2 Then
  8.   fFindHighestREC_ID = ID_1
  9. ElseIf ID_1 < ID_2 Then
  10.   fFindHighestREC_ID = ID_2
  11. Else    'both equal, assign to either one
  12.   fFindHighestREC_ID = ID_1
  13. End If
  14. End Function
It would seem to me that there is simpler way, maybe even an automated way, to have our file number reset to 0001 at the beginning of each year
__2. Automate the resetting of FILE_NO
  1. Create a Table called tblCurrentYear.
  2. Create a single Field in this Table called [Current Year] - INTEGER
  3. Enter the Current Year into the [Current Year] Field.
  4. Run the following code before any processing begins:
Expand|Select|Wrap|Line Numbers
  1. If Year(Now()) = DLookup("[Current Year]", "tblCurrentYear") Then
  2.    'do nothing, the year is the same
  3. Else      'a New Year
  4.    'run an UPDATE Query to Reset [FILE_NO] to Year(Now()) & "-0001"
  5. End If
Apr 25 '07 #2
tdw
206 New Member
__1. This process can be autuomated via a Public Function which returns the highest REC_ID in either 1 of 2 Tables and can be used virtually anywhere in the Database:
Expand|Select|Wrap|Line Numbers
  1. Public Function fFindHighestREC_ID() As Long
  2. Dim lngID_1 As Long, lngID_2 As Long
  3.  
  4. ID_1 = DLast("[REC_ID]", "SC_OPEN")
  5. ID_2 = DLast("[REC_ID]", "SC_ARCH")
  6.  
  7. If ID_1 > ID_2 Then
  8.   fFindHighestREC_ID = ID_1
  9. ElseIf ID_1 < ID_2 Then
  10.   fFindHighestREC_ID = ID_2
  11. Else    'both equal, assign to either one
  12.   fFindHighestREC_ID = ID_1
  13. End If
  14. End Function
__2. Automate the resetting of FILE_NO
  1. Create a Table called tblCurrentYear.
  2. Create a single Field in this Table called [Current Year] - INTEGER
  3. Enter the Current Year into the [Current Year] Field.
  4. Run the following code before any processing begins:
Expand|Select|Wrap|Line Numbers
  1. If Year(Now()) = DLookup("[Current Year]", "tblCurrentYear") Then
  2.    'do nothing, the year is the same
  3. Else      'a New Year
  4.    'run an UPDATE Query to Reset [FILE_NO] to Year(Now()) & "-0001"
  5. End If
I believe that I understand what both of those do. But currently the File Number is generated by a number within the code that is the highest REC_ID minus one. For example, if the highest REC_ID at the end of 2007 is, let's say, 45673, then we have to take that number, subtract 45672, so the end result is 1. Then it uses that 1 as the new file number. Might I add a second field to tblCurrentYear called HighREC_ID, and have the queries reference that field each time a new File Number is generated?
Then could I have the single record in tblCurrentYear update to the new year and new highest REC_ID at the beginning of each year?

Also, is there a better way to have the file number add the appropriate number of zeros to the beginning of the number (2007-0001, versus 2007-1; 2007-0234, versus 2007-234) than by using the four queries in a row as is currently happening?
Apr 25 '07 #3
tdw
206 New Member
I believe that I understand what both of those do. But currently the File Number is generated by a number within the code that is the highest REC_ID minus one. For example, if the highest REC_ID at the end of 2007 is, let's say, 45673, then we have to take that number, subtract 45672, so the end result is 1. Then it uses that 1 as the new file number. Might I add a second field to tblCurrentYear called HighREC_ID, and have the queries reference that field each time a new File Number is generated?
Then could I have the single record in tblCurrentYear update to the new year and new highest REC_ID at the beginning of each year?

Also, is there a better way to have the file number add the appropriate number of zeros to the beginning of the number (2007-0001, versus 2007-1; 2007-0234, versus 2007-234) than by using the four queries in a row as is currently happening?
Was my question confusing? Sorry.
May 1 '07 #4
NeoPa
32,572 Recognized Expert Moderator MVP
Was my question confusing? Sorry.
Your existing keys can be in either of two tables?

Conceptually you need to :
  • Get the current highest number from all the records that match the current year.
  • If there is none then you need to handle converting that result to 0. Nz() can work well in this scenario.
  • Add 1 to the result of your previous search.
  • Format this result if necessary (If you need to have n digits for instance).
  • Append this to your year and this should be your new key.
May 1 '07 #5
tdw
206 New Member
Your existing keys can be in either of two tables?

Conceptually you need to :
  • Get the current highest number from all the records that match the current year.
  • If there is none then you need to handle converting that result to 0. Nz() can work well in this scenario.
  • Add 1 to the result of your previous search.
  • Format this result if necessary (If you need to have n digits for instance).
  • Append this to your year and this should be your new key.
Thank you for the response. Please correct me if I am not understanding this correctly:
Are you saying that the above process should completely replace the current process for generating our file numbers? Meaning: get rid of all those existing queries, etc? And would that mean that I no longer even need the Rec_ID autonumber field? So, instead of using any kind of autonumber field whatsoever, I just let the file number creation process check for the highest existing value in the FILE_NO field for current year and add 1 each time a new order is created, thus when it's a new year it knows (because I tell it so) to start over at 0 (and then add 1).
I just want to be clear on this because this would be a total rewrite, and because it seems so much simpler than what currently exists I want to be sure that there isn't some reason that the guy who designed this database chose not to do it this way in the first place!

I am not familiar with Nz(), but I will find what I can learn on my own before asking you to explain it. :-)
May 2 '07 #6
NeoPa
32,572 Recognized Expert Moderator MVP
I'm very pleased to receive such an intelligent response tdw. I can't say that you should necessarily scrap all your old stuff as I don't know your database in detail. Certainly this is the way I'd approach a similar situation.
Thank you for the response. Please correct me if I am not understanding this correctly:
Are you saying that the above process should completely replace the current process for generating our file numbers? Meaning: get rid of all those existing queries, etc?
I think so. It may be possible that some of the queries etc provide some extra functionality, but as described I can't see what you'd need them for if you use the approach I laid out.
And would that mean that I no longer even need the Rec_ID autonumber field? So, instead of using any kind of autonumber field whatsoever, I just let the file number creation process check for the highest existing value in the FILE_NO field for current year and add 1 each time a new order is created, thus when it's a new year it knows (because I tell it so) to start over at 0 (and then add 1).
Yes.
Except that this should automatically detect that there are no records for a new year - therefore, your calculated result (Converted from null to 0 using Nz()) will still be appropriate to the situation. You shouldn't need to 'tell it so' explicitly.
I just want to be clear on this because this would be a total rewrite, and because it seems so much simpler than what currently exists I want to be sure that there isn't some reason that the guy who designed this database chose not to do it this way in the first place!
Not everyone is able to think the concept through, so many people try to implement it in the best way that they know how at the time.
I am not familiar with Nz(), but I will find what I can learn on my own before asking you to explain it. :-)
Excellent, but I'm happy to help further should you need.
May 2 '07 #7
tdw
206 New Member
I'm back! Ok here's what I've come up with but it's not working. First of all I don't know how to make the function suggested near the beginning of this thread work within a private sub. I'm sure it's easy and I'm just dumb, but oh well. So I tried the following, and am getting the message "you entered an invalid argument in a domain aggregate function". I'm guessing that it doesn't like the way I tried to narrow down the criteria for the DCount, but I'm not sure. Any ideas?
Expand|Select|Wrap|Line Numbers
  1. Dim stHighest As Integer
  2. Dim stHighOpen As Integer
  3. Dim stHighArch As Integer
  4.  
  5.     '   Check to see if this is the first order of the year
  6.     If DCount([FILE_NO], "SC_OPEN", Left([FILE_NO], 4) = Year(DATE)) = 0 Then
  7.         If DCount([FILE_NO], "SC_ARCH", Left([FILE_NO], 4) = Year(DATE)) = 0 Then
  8.             Me.FILE_NO = Year(DATE) & "-0001"   '   Reset file number to 1
  9.         End If
  10.     Else
  11.         '   Find the highest file number for the current year from both tables
  12.         stHighOpen = DMax(Right([FILE_NO], 4), "SC_OPEN", Left([FILE_NO], 4) = Year(DATE))
  13.         stHighArch = DMax(Right([FILE_NO], 4), "SC_ARCH", Left([FILE_NO], 4) = Year(DATE))
  14.         '   Pick the one that is highest
  15.             If stHighOpen < stHighArch Then
  16.                 stHighest = stHighArch
  17.             Else
  18.                 stHighest = stHighOpen
  19.             End If
  20.  
  21.         '   Create new file number
  22.         Me.FILE_NO = Year(DATE) & "-" & stHighest + 1
  23.     End If
  24.  
May 19 '07 #8
NeoPa
32,572 Recognized Expert Moderator MVP
Look at line #7
Expand|Select|Wrap|Line Numbers
  1. If DCount([FILE_NO], "SC_ARCH", Left([FILE_NO], 4) = Year(DATE)) = 0 Then
The first and third parameters should be a strings (enclosed in quotes (")) :
Expand|Select|Wrap|Line Numbers
  1. If DCount("[FILE_NO]", "SC_ARCH", "Left([FILE_NO], 4) = Year(DATE)) = 0 Then
Please check the rest of your code to make sure that it matches this rule.
May 19 '07 #9
tdw
206 New Member
Look at line #7
Expand|Select|Wrap|Line Numbers
  1. If DCount([FILE_NO], "SC_ARCH", Left([FILE_NO], 4) = Year(DATE)) = 0 Then
The first and third parameters should be a strings (enclosed in quotes (")) :
Expand|Select|Wrap|Line Numbers
  1. If DCount("[FILE_NO]", "SC_ARCH", "Left([FILE_NO], 4) = Year(DATE)) = 0 Then
Please check the rest of your code to make sure that it matches this rule.
I had a sneaky suspicion that it was something simple like that. Now I'm getting a new error "you canceled the last operation". I'll work on that one tomorrow though. If you know right off the top of your head what is wrong now please let me know, but otherwise I will try to figure this one out before asking the forum.

Thanks for the help!
May 20 '07 #10

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

Similar topics

28
3384
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task manager, that garbage collector doesn't actually do any collections unless the computer becomes low...
4
2365
by: pjac | last post by:
I need assistance in creating an Automatic tracking number that appears in a textbox that looks like: 2004-001. The first part of the number is based on the year, the second part is generated from the last available number used. Also, the second part of the number needs to start over whenever a new year is started. for example: 2005-001. Any help would be greatly appreciated. Thanks, pjac
3
17338
by: Mal | last post by:
Hello, Any advice on a function to convert a given week number to a date? Ideally I'd like the first day of that week. I'm using this to compare year to year....using the week number as the common factor. Mal.
3
1959
by: deltauser2006 | last post by:
My database consists of information which is updated every quarter. Forms will compare data from the present quarter to quarters past. I need a way to make the database save a copy of itself every quarter and then be able to have a form refer to that data through a comparison. The historical data set would be created at the end of each quarter and lock in all the values from that quarter. For example this quarter (1st quarter 2006) I...
1
2719
by: rdemyan via AccessMonster.com | last post by:
My App has 10 or so tables that we provide that contains proprietary data. This data will need to be updated once or twice a year. I would like some comments, suggestions on my proposed strategy for updating these tables via an ftp site: 1) Post a .mdb file to our ftp web site that contains the updated tables. My App code connects to the ftp site and gets the file name for any update files on the site. I already have code to do this...
3
1770
by: EllieT | last post by:
Hi all, I've been fiddling around with creating a new database (Access 2003, Windows XP) for work the past two weeks and am having trouble defining my own customized (yet still automatic) primary key. I have three different tables that need data inputted (Clients , Complaints , and Queries ) and need individual primary keys for each that are easily identifiable by the first letter and indicative year. I was hoping for something along...
10
1890
by: drrajnishpatel via AccessMonster.com | last post by:
i am trying to get "ID" an incremental number that simply resets to 1 each month in my "table1" thanks, -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/databases-ms-access/200808/1
14
19072
by: Tommy Jakobsen | last post by:
Hi. Is there a method in .NET that takes "year" as an argument and returns the total number of weeks in that year? For culture da-DK (Danish). Thanks in advance. Tommy.
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.