i am using the following code to generate a number in a text box on a form when data is entered into the form: - Private Sub Form_BeforeInsert(Cancel As Integer)
-
Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
-
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!
14 2311
You can use the DMax function's criteria to look for the maximum ticket number for the current year. - DMax("[ticketnum]", "master", "YearFieldName = '" _
-
& Format(Date,"yy") & "-'")
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?
@didacticone
I just threw this together off the top-of-my-head in work, but it may actually, in fact, work: - Create a Table named tblYear.
- This Table will consist of 1 Field only, named [Year] - {LONG}.
- Make this Table Hidden, so that it is not Visible to the average User.
- Execute the following code wherever appropriate.
- Dim lngStoredYear As Long
-
-
lngStoredYear = DLookup("[Year]", "tblYear")
-
-
If lngStoredYear = Year(Date) Then
-
'Year is the same, Increment by 1 and Format
-
Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
-
Else 'different Year, Reset TICKETNUM to 0001, Update [Year] in tblYear by 1 Year
-
Me![TICKETNUM] = "0001"
-
CurrentDb.Execute "Update tblYear Set tblYear.[Year] = " & lngStoredYear + 1, dbFailOnError
-
End If
- Any questions, feel free to ask.
Assuming the code you have works, just change - Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master"), 0) + 1, "0000")
to - Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master", _
-
"YearFieldName = '" & Format(Date,"yy") & "-'"), 0) + 1, "0000")
that code gives me a syntax error...
Which code and what error?
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
chipr... i used the code you gave me: - Me![TICKETNUM] = Format(Nz(DMax("[ticketnum]", "master",
-
"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!
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.
ok it worked but, when i changed the year to "10-" in the table as my default value... the form doesnt reset to 1
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.
so its actually not using the value i input in the year field in my table at all then?
No, it's not using your default value for anything here.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |