Question : Need VB code to Autonumber an Alphanumeric field...

Hello Experts!

I’m stuck!   I can't seem to find exactly what I need from EE solutions (or something that I can convert to what I need based on my limited knowledge of VB coding) - so I need your expertise!  

I’d like to allow the User to select a Filing Type from a pull down menu Field1 (or FileIDType_A in the Code/Image below) and have VB code populate Field2 (TempDocID in the Code/Image below) with a prefix from the Filing Type the user selected PLUS the next available number for that prefix.

For example:

If the User chose "Correspondence" from the Filing Type pull-down menu (FileIDType_A), then the "TempDocID" field would show C00001 if it was the 1st number in the category OR C00002 if it was the 2nd number, etc.

If the User chose "Document" from the Filing Type pull-down menu, then the "TempDocID' would return D00001.

Where I'm stuck is that the code I have built keeps returning "00001" and doesn't increment.  Another issue I want to avoid is: C00001, D00002, etc. - that the prefix is recognized and the counting is separate for each prefix (e.g. C00001, C00002, D00001, D00002).

Is that possible??  I've attached two Codes that I've been working with.  Code1 is the closest to what I need.  Code2 is quite raw - I just started on it and am including in case it is easier for you to convert it to what I need (it is only set up for one prefix).  I've also attached the Design View of my form to hopefully help.

THANK YOU!!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
CODE1

Private Sub File_TypeA_AfterUpdate()
    
    Dim FileType As String
    Dim MaxVal As String
    
    FileType = Me!FileIDType_A

    Select Case FileType
        Case "Correspondence"
            MaxVal = DMax("[TempDocID]", "T_DocList", "[In_PR_AR]=2")
            Me!TempDocID = "C" & Format(Val(Right(MaxVal, 5)) + 1, "00000")
        Case "Document"
            MaxVal = DMax("[TempDocID]", "T_DocList", "[In_PR_AR]=2")
            Me!TempDocID = "D" & Format(Val(Right(MaxVal, 5)) + 1, "00000")
        Case "Env Assessment"
            MaxVal = DMax("[TempDocID]", "T_DocList", "[In_PR_AR]=2")
            Me!TempDocID = "EA" & Format(Val(Right(MaxVal, 5)) + 1, "00000")
        Case "Map/Graphic"
            MaxVal = DMax("[TempDocID]", "T_DocList", "[In_PR_AR]=2")
            Me!TempDocID = "M" & Format(Val(Right(MaxVal, 5)) + 1, "00000")
        Case "Public/Agency Involvement"
            MaxVal = DMax("[TempDocID]", "T_DocList", "[In_PR_AR]=2")
            Me!TempDocID = "P" & Format(Val(Right(MaxVal, 5)) + 1, "00000")
        Case "Reference Library"
            MaxVal = DMax("[TempDocID]", "T_DocList", "[In_PR_AR]=2")
            Me!TempDocID = "R" & Format(Val(Right(MaxVal, 5)) + 1, "00000")
        Case Else
            Cancel = True
    End Select

End Sub


CODE2

Private Sub File_TypeA_AfterUpdate()
    
    Dim DB As DAO.Database
    Dim rst As DAO.Recordset
    Dim lngCntr As Long
    Dim intRetry As Integer
    Dim intNum As Integer, intA As Integer, intB As Integer
    Dim strANum As String

    On Error GoTo ErrorAlphaNumGenerate
    If IsNull(File_TypeA) Then
        Cancel = True
    Else
    Set DB = CurrentDb()
    Set rst = DB.OpenRecordset("T_Counter ", DB_OPEN_DYNASET)
    rst.MoveFirst
    rst.Edit
    rst!Value = rst!Value + 1
    rst.Update
    lngCntr = CLng(rst!Value) - 1
    intNum = lngCntr Mod 1000
    intA = (lngCntr \ 1000) Mod 26
    intB = (lngCntr \ 1000) \ 26

    strANum = Chr$(intB + 65) & Chr$(intA + 65) & Format$(intNum, "0000")
    AlphaNumGenerate = strANum
    Me!TempDocID = strANum

ExitAlphaNumGenerate:
    Exit Sub

ErrorAlphaNumGenerate:
    If Err = 3188 Then
        intRetry = intRetry + 1
        If intRetry < 100 Then
            Resume
        Else
            MsgBox Error$, 48, "Another user editing this number"
            Resume ExitAlphaNumGenerate
        End If
    Else
        MsgBox Str$(Err) & "   " & Error$, 48, "Problem Generating Number"
        Resume ExitAlphaNumGenerate
    End If
End If
End Sub
Attachments:
 
Access DB Design Screen
 

Answer : Need VB code to Autonumber an Alphanumeric field...

I didn't test it... I was just trying to point out that you need both your original condition and the 2nd condition.

dmax(something, something, "condition1 AND condition2")
Random Solutions  
 
programming4us programming4us