Results 1 to 8 of 8
  1. #1
    r3rpa is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2018
    Posts
    2

    Auto-Generate a Case ID using County, Year of Incident, and the amount of reports in a year

    Hello all,


    New to the forum and new to using Access, but I'm pretty sure the software is what I need to complete my task.

    I have a filing cabinet of paper-based incident reports that I need to create a database for. These reports already have a case number which is created by the county it took place in, the year the incident occurred, and the number the report resides within the year. For example: the fifth report filed in 2015, which occurred in Jones county would be "Jones 2015-05", the next incident that occurred that year, in Marshall county would be "Marshall 2015-06".

    By providing the county and the county of the incident date, I believe that this information should be easily generated in this format, and would be searchable by the department's case number. Does anyone have any suggestions how to create this workflow?

    Thanks

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What 'workflow' are you referencing? What exactly is your issue? How to programmatically create these IDs? Or just what the db structure should be? What other data is saved? Do you need related tables?

    As for saving the case number - two options. One is to save it as is in a single field. There other is to save the 3 parts into 3 fields. The latter is the most 'normalized' configuration.
    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
    r3rpa is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2018
    Posts
    2
    Sorry for the ambiguity. Rather than access create a auto-generated number I would like it to create an auto generated case number that is created using the county, year, and the incident number within the incident year. Access likes to make an auto-generated number that starts at zero, but I'd like to make it follow the former option.

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'd probably still just use Access's autonumber field as the primary key for the records.

    Will the case records have a date field anyway that has full date of incident? mm/dd/yy? Does the year of this date field 100% of the time and always and forever correspond to the YYYY in your Case ID format given above?

    What about splitting your "human readable ID" into two fields? Have one field that is a foreign key field that references a table that lists all the counties.
    Then the second a string field that has a unique index that contains strings in the format of "yyyy-nn".

    You can use VBA to autoincrement the string field when creating new records. I have a generic function I wrote to do just that if you're interested.

    As for searching by this case number, it will be easy enough to create a serach form to search by these fields but I don't think you're at that stage of the project yet.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Generating custom unique identifier is frequent topic. Search forum to review numerous threads. Here is one https://www.accessforums.net/showthread.php?t=23329

    Example search form http://allenbrowne.com/ser-62.html
    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.

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Here is my attempt to recreate the Ruby programming language's succ() function in VBA. Succ is a generic function that will increment a string. I've also attached an example database.

    succ() public Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set’s collating sequence.
    If the increment generates a “carry,” the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary.
    https://apidock.com/ruby/String/succ

    Code:
    Option Compare Database
    Option Explicit
    
    'succ increments an alphanumeric string, returns the (succ)essor to str
    '   this is my implementation of ruby's String.succ / String.next method
    '   it will only increment the right most group of alphanumeric characters
    '   it will add one character if necessary eg 2 char input "ZZ" becomes 3 chars "AAA"
    '   or "99" becomes "100".
    '   Numbers always increment to the next number, letters to the next letter.
    '   https://ruby-doc.org/core-2.3.1/String.html#method-i-next
    Public Function succ(ByVal str) As Variant
    On Error GoTo ErrHandler
        Dim str_len As Integer 'input string's length
        Dim i As Integer       'index as we loop through input string
        Dim b As Byte          'ascii byte value of character we're evaluating
        Dim b_prev As Byte     'ascii byte of last alphanumeric character evaluated
        Dim carry As Boolean   'does the loop need to keep going?
        
        str = " " & str 'insert space to trick for loop to behaving correctly when we have carry over after index = 1 and avoid extra error handling
        str_len = Len(str)
        b_prev = False
        carry = True
        
        For i = str_len To 1 Step -1    'Loop through the string backwards (right to left), i = current index of loop
            b = Asc(Mid(str, i, 1))     'b = the ascii value of the character at string index i
            
            Select Case b
                Case 65 To 90, 97 To 122, 48 To 57  'case if A to Z, a to z, or 0 to 1 (alphanumerics)
                    b = b + 1                       'we found a alphanumeric so increment it
                    
                    'If the increment needs to roll back then handle it. eg Z increments back to A
                    ' if there is a roll back (or carry over) then we will need to continue the for loop to the next char
                    Select Case b
                        Case 91
                            b = 65 '=A
                        Case 123
                            b = 97 '=a
                        Case 58
                            b = 48 '=0 (zero)
                        Case Else
                            'There was no carry over, we're already almost done
                            carry = False
                    End Select
                    
                    'replace the character at index i with the incremented value
                    Mid(str, i, 1) = Chr(b)
                    
                    'if there was no carry over then we're done, exit loop. otherwise repeat process until no more carry overs
                    If Not carry Then Exit For Else b_prev = b
                    
                Case Else
                    'either we havn't hit an alphanumeric yet in our right to left scan
                    ' - OR - we need to add an additional character. Eg given input string "9" increments and rolls back to "0" BUT
                    ' we need to insert another character in this case so result equals "10", not just "0". Insert that char into
                    ' the string here.
                    If b_prev Then 'we need to insert a new character
                        Select Case b_prev
                            Case 65 'A
                                b = 65
                            Case 97 'a
                                b = 97
                            Case 48 '0 (zero)
                                b = 49
                        End Select
                        str = Left(str, i) & Chr(b) & Right(str, str_len - i)
                        Exit For ' We are done
                    End If
            End Select
        Next i
        
        'return results without the extra space we inserted at the beginning of the function
        '   recalcualte len(str) because we could have inserted another character since we started
        succ = Right(str, Len(str) - 1)
        
    ExitHandler:
        Exit Function
        
    ErrHandler:
        Debug.Print "Error in 'succ' function. Error #" & Err.Number & ": " & Err.Description
        succ = Err
        Resume ExitHandler
        
    End Function
    SuccExample.accdb

  7. #7
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Sorry for the ambiguity. Rather than access create a auto-generated number I would like it to create an auto generated case number that is created using the county, year, and the incident number within the incident year. Access likes to make an auto-generated number that starts at zero, but I'd like to make it follow the former option.
    It may make more sense to use an auto number for PK and store the county and incident number separately. I'm assuming your storing an incident date as well, which should be sufficient to provide the year. You can always concatenate these values to get the format you want for display. This would also make it easier to search for instance for all incidents in Jones county in 2017 or to get a count of all incidents in each county for a given year.

    Seeing as your converting a cabinet of existing reports I'm not sure you would want to auto increment the incident numbers. You will also need a procedure to go to 1 at the beginning of each year if you do.

  8. #8
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by moke123 View Post
    It may make more sense to use an auto number for PK and store the county and incident number separately. I'm assuming your storing an incident date as well, which should be sufficient to provide the year.
    An issue I see with that is enforcing, assuming they want to, uniqueness between the combination of the sequence number and the year part of the date field.

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

Similar Threads

  1. Replies: 8
    Last Post: 08-21-2017, 11:13 AM
  2. Replies: 2
    Last Post: 04-19-2017, 01:09 PM
  3. generate Report Borrow Book By Month and Year
    By ultra5219 in forum Reports
    Replies: 2
    Last Post: 08-07-2016, 06:57 AM
  4. Replies: 1
    Last Post: 10-14-2015, 03:45 PM
  5. Replies: 3
    Last Post: 06-22-2015, 06:36 AM

Tags for this Thread

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