Results 1 to 6 of 6
  1. #1
    jwaes is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    7

    Question Question about calculated field in format yyww-001

    I have a projects-table with an autoNumber pk, but next to that i want to add the projectCode we use internally.


    Such a projectCode has the format of two digits for the year 'yy', two digits for the week of the year, but with a leading zero if < 10 'ww', a dash and a 3 digit counter.

    So the first project of this week would be
    1918-001
    the second
    1918-002
    the first project of next week would be
    1919-001

    ideally the field is automatically calculated when a record is created.

    Is something like this possible ??

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    How exactly are new records being created?
    Through an entry form, or through data import?

    If through an entry form, you could add VBA code that runs upon creation of a new record.
    If being imported, you would either need to run an Update Query and/or VBA code to populate all the necessary values.

  3. #3
    jwaes is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    7
    It would be using an entry form

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    OK, you would create some VBA code that runs on Insert of a new record from your Form (either the "BeforeInsert" or "AfterInsert" event). I was working on it, but it appears that my workplace install of Access is corrupt, so I have not been able to work through all the kinks yet. But here is the VBA code that you can try to work with.

    Note for this code, I assumed the following:
    Table name is "MyTable"
    Autonumber field is "ID"
    Project Code field is "projectCode".
    You may need to adjust, where necessary.
    Code:
    Private Sub Form_BeforeInsert(Cancel As Integer)
    
        Dim recCount As Long
        Dim newNum As Long
        Dim lastID As Long
        Dim projCode1 As String
        Dim projCode2 As String
        
    '   Check to see if this is first record added to table...
        recCount = DCount("*", "MyTable")
        If recCount = 0 Then
            newNum = 1
        Else
    '       ...otherwise get projectCode of most recent record
            lastID = DMax("ID", "MyTable")
            projCode1 = DLookup("projectCode", "MyTable", "ID=" & lastID)
    '       See if last project code is from this week...
            If Left(projCode1, 4) = Format(Date, "yyww") Then
    '           ...if it is, add 1 to current number
                newNum = Right(projCode1, 3) + 1
    '           ...otherwise, return 1
                newNum = 1
            End If
        End If
        
    '   Create new code and apply
        projCode2 = Format(Date, "yyww") & "-" & Format(newNum, "000")
        Me.projectCode = projCode2
    
    End Sub

  5. #5
    jwaes is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    7

    Thumbs up

    Thanks so much for your help.

    I changed my Table and Field details
    Added an 'Else' before the newNum = 1 line

    Added
    Dim yearWeek As String
    yearWeek = Format(Date, "yy") & Format(Format(Date, "ww"), "00")
    So for week 3
    it would show as 1903 instead of 193

    But you got me started. Thanks so much

  6. #6
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome.

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

Similar Threads

  1. Can I format a field used in a Calculated field
    By Phoenixenv in forum Access
    Replies: 2
    Last Post: 09-25-2017, 09:18 AM
  2. Calculated field question
    By Hello1 in forum Access
    Replies: 3
    Last Post: 12-16-2015, 06:43 PM
  3. Table--field--format question
    By vickimurray in forum Access
    Replies: 4
    Last Post: 07-31-2015, 11:39 AM
  4. Format numbers for a calculated field in a query?
    By Access_Novice in forum Queries
    Replies: 2
    Last Post: 10-25-2014, 03:56 PM
  5. Calculated Field on form (beginner question)
    By Williams485 in forum Access
    Replies: 21
    Last Post: 02-26-2013, 09:02 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