473,444 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Incrementing a field without Autonumber

Hi Everyone,

I know this topic has been posted a lot, so I apologize for having to post another one, but the answers I've been finding seem to be more advanced than I know how to use.

I have a table called [MOC Tracking Log], with a field named [MOC ID Number]. The general format for this number is supposed to be (yyyy-0001). The last 4 digits are supposed to increment each time a new record is created (ie, 2007-0002, 2007-0003 etc). I have found some useful code (i think) to do the incrementation part itself. However:

1) I don't know how to set the original format to be (yyyy-0001)

2) I don't know where to put the code.

So far, I have this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Format (Date = "yyyy")
  4.  
  5.  
  6. Dim tdf As TableDef
  7. Dim fld As Field
  8. Dim num As Double
  9.  
  10. num = "0001"
  11.  
  12. Set tdf = CurrentDb.TableDefs("[MOC Tracking Log]")
  13. Set fld = tdf.Fields("[MOC ID Number]")
  14.  
  15. fld.Value = (Date & "-" & num)
  16.  
  17. fld = ("SELECT Max(Right([MOC ID Number])) As MaxNum FROM [MOC Tracking Log]")
  18.  
  19.     num = fld!MaxNum + 1
  20.  
  21. End Sub
  22.  
which is basically a random jumble of code snippets I've found and can only somewhat make sense of.

Any clarification, or links to understandable tutorial sites would be greatly appreciated.

Thanks,
Tiffany
Aug 27 '07 #1
5 3432
JConsulting
603 Expert 512MB
Hi Everyone,

I know this topic has been posted a lot, so I apologize for having to post another one, but the answers I've been finding seem to be more advanced than I know how to use.

I have a table called [MOC Tracking Log], with a field named [MOC ID Number]. The general format for this number is supposed to be (yyyy-0001). The last 4 digits are supposed to increment each time a new record is created (ie, 2007-0002, 2007-0003 etc). I have found some useful code (i think) to do the incrementation part itself. However:

1) I don't know how to set the original format to be (yyyy-0001)

2) I don't know where to put the code.

So far, I have this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Format (Date = "yyyy")
  4.  
  5.  
  6. Dim tdf As TableDef
  7. Dim fld As Field
  8. Dim num As Double
  9.  
  10. num = "0001"
  11.  
  12. Set tdf = CurrentDb.TableDefs("[MOC Tracking Log]")
  13. Set fld = tdf.Fields("[MOC ID Number]")
  14.  
  15. fld.Value = (Date & "-" & num)
  16.  
  17. fld = ("SELECT Max(Right([MOC ID Number])) As MaxNum FROM [MOC Tracking Log]")
  18.  
  19.     num = fld!MaxNum + 1
  20.  
  21. End Sub
  22.  
which is basically a random jumble of code snippets I've found and can only somewhat make sense of.

Any clarification, or links to understandable tutorial sites would be greatly appreciated.

Thanks,
Tiffany


Form load initially, then you would want this each time you started a new record.
Expand|Select|Wrap|Line Numbers
  1. dim MyCount as long
  2. if dcount("*","[MOC Tracking Log]")=0 then
  3. me.[MOC ID Number].value = Format(Date(),"yyyy") & "-0001"
  4. else
  5. MyCount = myval = Right(DMax("[MOC ID Number]", "[MOC Tracking Log]"), 4)
  6. me.[MOC ID Number].value = Format(Date(),"yyyy") & mycount
  7. end if
  8.  
  9.  
J
Aug 28 '07 #2
hyperpau
184 Expert 100+
Hi Everyone,

I know this topic has been posted a lot, so I apologize for having to post another one, but the answers I've been finding seem to be more advanced than I know how to use.

I have a table called [MOC Tracking Log], with a field named [MOC ID Number]. The general format for this number is supposed to be (yyyy-0001). The last 4 digits are supposed to increment each time a new record is created (ie, 2007-0002, 2007-0003 etc). I have found some useful code (i think) to do the incrementation part itself. However:

1) I don't know how to set the original format to be (yyyy-0001)

2) I don't know where to put the code.

So far, I have this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Format (Date = "yyyy")
  4.  
  5.  
  6. Dim tdf As TableDef
  7. Dim fld As Field
  8. Dim num As Double
  9.  
  10. num = "0001"
  11.  
  12. Set tdf = CurrentDb.TableDefs("[MOC Tracking Log]")
  13. Set fld = tdf.Fields("[MOC ID Number]")
  14.  
  15. fld.Value = (Date & "-" & num)
  16.  
  17. fld = ("SELECT Max(Right([MOC ID Number])) As MaxNum FROM [MOC Tracking Log]")
  18.  
  19. num = fld!MaxNum + 1
  20.  
  21. End Sub
  22.  
which is basically a random jumble of code snippets I've found and can only somewhat make sense of.

Any clarification, or links to understandable tutorial sites would be greatly appreciated.

Thanks,
Tiffany
Change this:
Expand|Select|Wrap|Line Numbers
  1.  fld.Value = (Date & "-" & num)
  2.  
to this
Expand|Select|Wrap|Line Numbers
  1.  fld.Value = (Format(Date, "yyyy") & "-" & num)
  2.  
Aug 28 '07 #3

Expand|Select|Wrap|Line Numbers
  1. dim MyCount as long
  2. if dcount("*","[MOC Tracking Log]")=0 then
  3. me.[MOC ID Number].value = Format(Date(),"yyyy") & "-0001"
  4. else
  5. MyCount = myval = Right(DMax("[MOC ID Number]", "[MOC Tracking Log]"), 4)
  6. me.[MOC ID Number].value = Format(Date(),"yyyy") & mycount
  7. end if
  8.  
Thanks for the help, it seems to be working so far, with the exception of a minor compiler issue. Could you also tell me what the proper format is to replace "Me." for a table and field?

Thanks,
Tiffany
Aug 28 '07 #4
JConsulting
603 Expert 512MB
Thanks for the help, it seems to be working so far, with the exception of a minor compiler issue. Could you also tell me what the proper format is to replace "Me." for a table and field?

Thanks,
Tiffany
Hey,
replacing as in putting a variable in place of??

forms(sForm).form(sField).value for a simple form. Subforms are different.
Aug 28 '07 #5
Ok thanks, I'll try that

Tiffany
Aug 28 '07 #6

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

Similar topics

7
by: Amy | last post by:
I'm trying to add an autoincrementing id to a table based on an existing field Name, but Name has duplicated records. How can I do that in ACCESS? Thanks. Amy
6
by: HD | last post by:
Hello. For the following, I would appreciate if anyone could tell me: if it can be done, how it might done, and/or what search terms I could use to find the solution myself. I would like to...
2
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings...
3
by: ben.werdmuller | last post by:
Hi, Is there an easy way in ASP/VBscript to grab an autonumber (primary key) field just after an SQL insert? This is probably easy, but I'm stuck .. Cheers.
1
by: gtwannabe | last post by:
I'm having a problem with a form that uses AutoNumber as the primary key. I have an Abort button to delete the current record and close the form. If AutoNumber is assigned, the code executes a...
1
by: dympna | last post by:
HI I am trying to create a number field that will create a unique incremented number each time a record is added to the database via a form. but I want that number to be in sequence and start from...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
1
by: asandiego | last post by:
Hey guys, this is my first post here but have been checking this site a lot for anything I need. I hope someone can lead me to what I should do or just an idea to what can be done. What I'm...
3
by: bkberg05 | last post by:
Hi - I have a field called Sequence on a continuous subform. The Sequence field belongs to a table called Project_Draw. This table has many 'Draw' records for each 'Project' record. So then the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.