Results 1 to 5 of 5
  1. #1
    accessLearn is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    11

    how to select month+year based on current year in VBA


    I have a form in access where I need to generate employee_Id based on employment_dt in the format month(employment_dt)+Year(Employment_dt)-001 (3 digit number incrementing 1)
    I need to select max(monthand year)based on the most recent year but dmax(employment_dt) selects based on max(month)
    Ex-
    If I have 1214 and 0115, I want to select 0115 and not 1214 but getting 1214.
    How can I do that?

    Thanks!!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Generating custom unique identifier is a common topic.

    Basing an employee ID on a date seems poor schema to me. What if an employee is re-hired?

    Probably get 1214 because that number is greater than 0115.

    If you want records to filter/sort chronologically, put year first: 1501, 1412
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    accessLearn is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jul 2015
    Posts
    11
    I do not have the option of putting year first and then month as it is the way it has been setup and ids have been provided to employees.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Post the code you are using.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    What are they using now to generate these numbers? Are you developing new code or what?

    You say you need this for a form. Is this how it is stored in the table also?

    You may need some code/function to manipulate the data to get what you need.

    Code:
    Sub test1()
    
        Dim M As Integer, Y As Integer
        Dim forDisplay As String, ForSorting As Integer
        M = Month(Date)
        Y = Year(Date)
        forDisplay = Format(M, "00") & Right(Y, 2)  'keep the leading 0 and use string
        ForSorting = CInt(Right(Y, 2) & Format(M, "00")) 'sort it by year, then month numeric sort
        Debug.Print forDisplay & vbCrLf & ForSorting
    End Sub
    
    'Gives following result
    '0715
    '1507
    Last edited by orange; 07-13-2015 at 02:22 PM. Reason: added code to illustrate concept

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

Similar Threads

  1. Replies: 3
    Last Post: 06-22-2015, 06:36 AM
  2. Get first day and month of current year
    By Ruegen in forum Programming
    Replies: 3
    Last Post: 12-01-2014, 06:45 PM
  3. Query for current month and year onward
    By tylerg11 in forum Queries
    Replies: 1
    Last Post: 12-30-2013, 12:10 PM
  4. Current Month/Year Query
    By Roadbeer in forum Queries
    Replies: 3
    Last Post: 06-17-2013, 01:20 PM
  5. Current Month and Year-To-Date
    By DSnipeFunk in forum Access
    Replies: 4
    Last Post: 05-31-2011, 11:38 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