Solved

Custom autonumber format in access

Posted on 2013-12-01
9
2,727 Views
Last Modified: 2013-12-02
I am trying to have custom autonumber and found the below url helpful.

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_23238522.html

Is there anyone who can help to make it in this format JBddmmyy-1 or JBddmmyyxx
eg. JB021213-01, JB021213-02, JB021213-03
or
eg. JB02121301, JB02121302, JB02121303
i.e each day it will start from 01 with that date format
eg. JB031213-01, JB031213-02, JB031213-03  for tomorrow


Is there anyone who can make the changes and upload it here or post the code here
0
Comment
Question by:-MAS
  • 4
  • 3
  • 2
9 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Hi,

pls try

Function getNextNo() As String
Dim intMax As Integer, curVal, newVal, yr As String
theDate = Format(Date, "ddmmyy")
curVal = Nz(DMax("RandomNum", "Table1")) 'Change to your table
If Mid(curVal, 3, 6) = theDate Then
    If Len(curVal & "") > 0 Then
        intMax = Left(curVal, 2)
        newVal = theDate & "-" & Format(intMax + 1, "00")
        Else
        newVal = theDate & "- 01"
    End If
    Else
    newVal = theDate & " - 01"
End If
getNextNo = "JB" & newVal
End Function

Open in new window

Regards
0
 
LVL 24

Author Comment

by:-MAS
Comment Utility
Thanks for your code.
Can you upload an access2007/2010 DB as I tried and ended up in error.

Appreciate your help
0
 
LVL 29

Accepted Solution

by:
IrogSinta earned 250 total points
Comment Utility
Here's another function you could try out:
Public Function GetNextSeq() As String
    Dim bSeq As Byte
    Dim sPrefix As String
    
    sPrefix = Format(Date, "JBddmmyy")
    bSeq = Nz(DMax("Right([ID],2)", "TableName", "[ID] LIKE '" & sPrefix & "*'"), 0) + 1
    GetNextSeq = sPrefix & Format(bSeq, "-00")
    
End Function

Open in new window

Just be sure that the datatype of your field is set to TEXT.

Ron
0
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 total points
Comment Utility
Hi,

Corrected code

Function getNextNo() As String
Dim intMax As Integer, curVal, newVal, yr As String
theDate = Format(Date, "ddmmyy")
curVal = Nz(DMax("RandomNum", "Table1")) 'Change to your table
If Mid(curVal, 3, 6) = theDate Then
    If Len(curVal & "") > 0 Then
        intMax = Right(curVal, 2)
        newVal = theDate & "-" & Format(intMax + 1, "00")
        Else
        newVal = theDate & "-01"
    End If
    Else
    newVal = theDate & "-01"
End If
getNextNo = "JB" & newVal
End Function

Open in new window

EDIT

Function getNextNo() As String
Dim intMax As Integer, curVal, newVal, yr As String
theDate = Format(Date, "ddmmyy")
curVal = Nz(DMax("[RandomNum]", "Table1", "[RandomNum] Like 'JB" & theDate & "*'"))   'Change to your table
If Mid(curVal, 3, 6) = theDate Then
    If Len(curVal & "") > 0 Then
        intMax = Right(curVal, 2)
        newVal = theDate & "-" & Format(intMax + 1, "00")
        Else
        newVal = theDate & "-01"
    End If
    Else
    newVal = theDate & "-01"
End If
getNextNo = "JB" & newVal
End Function

Open in new window

Regards
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 24

Author Comment

by:-MAS
Comment Utility
This is the error I am getting now
"You can't go to the specified record"

and the below line is highlighted in yellow
DoCmd.GoToRecord , , acNewRec
0
 
LVL 24

Author Closing Comment

by:-MAS
Comment Utility
Many thanks to both
I appreciate if you can explain what your code does.
Just want to know. Please explain when you are free.
0
 
LVL 29

Expert Comment

by:IrogSinta
Comment Utility
The following line formats the date as ddmmyy with JB at the beginning.  For instance, todays' date is stored in the variable sPrefix as JB021213:
sPrefix = Format(Date, "JBddmmyy")

This next line looks only at the records in your table where your ID field begins with the same prefix as what is stored in the variable (using the LIKE keyword and a wildcard '*').  It gets the maximum value of the last 2 characters and adds 1 to that value.  If there is no record yet for the day, the NZ function tells it to return a 0 (which ends up having a 1 added to it).
bSeq = Nz(DMax("Right([ID],2)", "TableName", "[ID] LIKE '" & sPrefix & "*'"), 0) + 1

Finally, this line returns the prefix concatenated with the next sequence number wich is formatted to show two numeric places and a dash at the beginning.
GetNextSeq = sPrefix & Format(bSeq, "-00")

Ron
0
 
LVL 24

Author Comment

by:-MAS
Comment Utility
I am confused of which will check whether the record is there or no?

Can you post the code which will check the for record availability.
if the record there which coding adding +1
0
 
LVL 29

Expert Comment

by:IrogSinta
Comment Utility
If the record doesn't exist for that day, the DMax function will return a NULL value.  Hence Nz(DMax("Right([ID],2)", "TableName", "[ID] LIKE '" & sPrefix & "*'"), 0) will become Nz(NULL, 0).  The Nz function in this instance will default to 0 and since we always add 1 to the result, you will get a Sequence starting at 1 whenever the record doesn't exist.

Ron
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now