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

self generating number

266 256MB
i am using the following code to generate a number in a text box on a form when data is entered into the form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.   Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
  3. End Sub
what i would like to know is, is there any way that when the new year comes the number will restart at 0001. i do have a year field in my table as it is the first part of our unique ticket number system.. the number would display as
"09-0001" with "09-" being the default value for the year field that i will change every year. hope thats enough info to make sense... any help is greatly appreciated. thanks!
Feb 6 '09 #1
14 2311
ChipR
1,287 Expert 1GB
You can use the DMax function's criteria to look for the maximum ticket number for the current year.

Expand|Select|Wrap|Line Numbers
  1. DMax("[ticketnum]", "master", "YearFieldName = '" _
  2. & Format(Date,"yy") & "-'")
Feb 6 '09 #2
didacticone
266 256MB
ok very newb question here...but where would i place that code... and do i have to eliminate the default value in my year field on the table?
Feb 6 '09 #3
ADezii
8,834 Expert 8TB
@didacticone
I just threw this together off the top-of-my-head in work, but it may actually, in fact, work:
  1. Create a Table named tblYear.
  2. This Table will consist of 1 Field only, named [Year] - {LONG}.
  3. Make this Table Hidden, so that it is not Visible to the average User.
  4. Execute the following code wherever appropriate.
    Expand|Select|Wrap|Line Numbers
    1. Dim lngStoredYear As Long
    2.  
    3. lngStoredYear = DLookup("[Year]", "tblYear")
    4.  
    5. If lngStoredYear = Year(Date) Then
    6.   'Year is the same, Increment by 1 and Format
    7.   Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
    8. Else    'different Year, Reset TICKETNUM to 0001, Update [Year] in tblYear by 1 Year
    9.   Me![TICKETNUM] = "0001"
    10.   CurrentDb.Execute "Update tblYear Set tblYear.[Year] = " & lngStoredYear + 1, dbFailOnError
    11. End If
  5. Any questions, feel free to ask.
Feb 6 '09 #4
ChipR
1,287 Expert 1GB
Assuming the code you have works, just change
Expand|Select|Wrap|Line Numbers
  1. Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
to
Expand|Select|Wrap|Line Numbers
  1. Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master", _
  2.      "YearFieldName = '" & Format(Date,"yy") & "-'"), 0) + 1, "0000")
Feb 6 '09 #5
didacticone
266 256MB
that code gives me a syntax error...
Feb 6 '09 #6
ChipR
1,287 Expert 1GB
Which code and what error?
Feb 6 '09 #7
didacticone
266 256MB
adezii... i tried your way and placed that code in the 'beforeinsert' event of the form and it returns run time error '94': invalid use of null. and it highlights the line:

lngStoredYear = DLookup("[Year]", "tblYear")

any ideas? and thanks to each of you for your help
Feb 6 '09 #8
didacticone
266 256MB
chipr... i used the code you gave me:
Expand|Select|Wrap|Line Numbers
  1. Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master", 
  2. "YearFieldName = '" & Format(Date,"yy") & "-'"), 0) + 1, "0000")
after i paste it in vb it is all red.. and when i change the year field name to my year fields name it returns:

compile error:
expected:line number or label or statement or end of statement

thanks!
Feb 6 '09 #9
ChipR
1,287 Expert 1GB
Is it really two lines? Because if it is, you need to continue it with _ &
along with putting in the correct name of your Year field.
Feb 6 '09 #10
didacticone
266 256MB
ok it worked but, when i changed the year to "10-" in the table as my default value... the form doesnt reset to 1
Feb 6 '09 #11
ChipR
1,287 Expert 1GB
Why would it reset to 1? It's looking up the highest ticket number for the current year in the table and adding 1 to it.
Feb 6 '09 #12
didacticone
266 256MB
so its actually not using the value i input in the year field in my table at all then?
Feb 6 '09 #13
ChipR
1,287 Expert 1GB
No, it's not using your default value for anything here.
Feb 6 '09 #14
didacticone
266 256MB
oh ok thats where i was confused... so now when 2010 begins the number will restart at 0001? that is awesome.. thanks a ton for your help
Feb 6 '09 #15

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

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
37
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
7
by: eric.gagnon | last post by:
In a program randomly generating 10 000 000 alphanumeric codes of 16 characters in length (Ex.: "ZAZAZAZAZAZAZ156"), what would be an efficient way to ensure that I do not generate duplicates? ...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
0
by: jacquesvdz | last post by:
Hi guys/girls I already post this in javascript forum, but nobody could help me.C# guys are always sharp and fast, so I am trying ny Luck:-) I wonder who can help me: I know sort of how...
7
by: comp.lang.php | last post by:
<? class EditResultGenerator extends MethodGeneratorForActionPerformer { /** * Create an instance of itself if $this does not yet exist as an EditResultGenerator class object * * @access...
5
by: openopt | last post by:
I have class A: def __init__(self, objFun, x0): #(I want to have self.primal.f = objFun) #both self.primal.f = objFun #and self.primal = None self.primal.f = objFun
1
by: sparks | last post by:
we are generating numbers for the data collection people. this worked fine for a while.. Private Sub generatenumber() Dim db As DAO.Database Dim rs As DAO.Recordset Dim StudentNumber As...
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...

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.