Solved

Custom autonumber format in access

Posted on 2013-12-01
9
2,937 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39689211
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 25

Author Comment

by:-MAS
ID: 39689250
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
ID: 39689267
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 50

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 250 total points
ID: 39689279
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
 
LVL 25

Author Comment

by:-MAS
ID: 39689512
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 25

Author Closing Comment

by:-MAS
ID: 39689523
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
ID: 39691451
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 25

Author Comment

by:-MAS
ID: 39691791
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
ID: 39691830
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 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