Results 1 to 8 of 8
  1. #1
    nishant.dhruve is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    34

    Autonumber problem

    i have attached a picture and a code that i m using for access.


    so when i press a command button its generates a number having a format
    EU17/0000

    But i m having problem, instead of increment of the number in EU17/0001, EU17/0002,
    It being increasing like EU17/0000, EU17/EU17/0000, EU17/EU17/EU17/0000......

    Can someone helo me on this to correct the increment please
    [Code]
    Private Sub btnmakeID_Click()


    vID = Nz(DMax("[ID]", "SapRequest"))


    ID = "EU" & Right(Date, 2) & "/" & Format(vID, "0000")




    End Sub
    [Code]


    Click image for larger version. 

Name:	MS access ID.PNG 
Views:	16 
Size:	22.3 KB 
ID:	27858Click image for larger version. 

Name:	Capture.PNG 
Views:	18 
Size:	24.0 KB 
ID:	27859

  2. #2
    Forbes's Avatar
    Forbes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Mar 2017
    Posts
    133
    Does it work when you put it in like so "0001/EU17"

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    try running your code in steps. You have a text ID which cannot be formatted (formatting only applies to numbers) so you are getting

    vid=max....="EU17/0000"

    formatting vid (EU17/0000) returns vid - EU17/0000

    ID = "EU" & Right(Date, 2) & "/" & Format(vID, "0000") =EU17/EU17/0000

    so you need to extract the last 4 characters from vid, convert it to a number and then format it again

    You would be much better keeping this as 3 separate fields - but I guess you've probably been told that in the past

  4. #4
    nishant.dhruve is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    34
    no its doesn't work in that way too

  5. #5
    nishant.dhruve is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Aug 2016
    Posts
    34
    i didnt get you quite well.
    Would be able to help by modifying the code the way you are telling to me.
    it would be more easy to understand

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    If you are referring to me then no, not really. I don't believe in providing solutions that are not the right way to do things. You know how to use the right string function and the format function. The other one you will need is the val function

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    This is the wrong way to approach this as ajax has said. You should use an autonumber as your primary key. You then have two options:

    1. set the display format of the autonumber field to be "EU17/" & AutonumberfieldName any time you need to display it. This may give you non sequential numbers
    2. Keep your autonumber primary key field but make this other field a non primary key index field.

    The syntax you want is

    Code:
    dim sMaxID as string
    dim sNewMaxID as string
    
    sMaxID = dmax("[ID]", "SAPRequests")
    snewmaxid = left(smaxid, 5) & right("0000" & cint(right(smaxid, len(smaxid) - 5))
    just bear in mind this will bomb out after you hit record 9999

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    the other problem you will have with the dmax is if you have other prefixes to EU

    and as an autonumber - if this is indexed and/or is your primary key, the indexes are over larger and so slower to search - a long number takes 4 bytes - your text takes 13 - so over 3 times slower.

    Your technique is confusing presentation with data storage - an Excel 'style' which combines presentation and storage into one view - which is consequently not normalised and inefficient from a data storage and retrieval perspective.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. autonumber
    By Dave_D in forum Queries
    Replies: 2
    Last Post: 12-27-2016, 10:27 AM
  2. MS Access 2010 AutoNumber Problem
    By jjfaith in forum Queries
    Replies: 5
    Last Post: 08-25-2016, 10:08 AM
  3. Replies: 4
    Last Post: 11-21-2013, 05:06 PM
  4. Autonumber problem
    By tony6562 in forum Programming
    Replies: 3
    Last Post: 05-28-2012, 03:23 PM
  5. Autonumber Problem in adp project
    By dneruck in forum Access
    Replies: 8
    Last Post: 04-22-2010, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums