I have a field that I use to show the records based on a number. It has an A followed by a zero followed by five numbered digits. I need to use this format since there is already data in the table in this format and if we change it it will be confused with another field in the table.
The question I have is how do I get the code listed below to auto-generate the next sequential number when a new record is added. Currently it adds a new record with the same number for every new record.
This is in the On Current event for the form:
Private Sub Form_Current()
If Me.NEWRECORD Then
On Error Resume Next
Me.NMMI_NUM = "A" & pad_zeros(Val(Mid$(DMax("[NMMI_NUM]", "NMPHY"), 2)) + 1, "right", 6)
End If
End Sub
NMMI_NUM is the field, NMPHY is the table
This is in the Module:
Function pad_zeros(number, align, digits)
Dim num As String, s As String
num = Trim$(Str$(number))
If digits = Len(num) Then
s = num
Else
Select Case LCase$(align)
Case "left"
s = num + String$(digits - Len(num), "0")
Case "right"
s = String$(digits - Len(num), "0") + num
End Select
End If
pad_zeros = s
End Function
The question I have is how do I get the code listed below to auto-generate the next sequential number when a new record is added. Currently it adds a new record with the same number for every new record.
This is in the On Current event for the form:
Private Sub Form_Current()
If Me.NEWRECORD Then
On Error Resume Next
Me.NMMI_NUM = "A" & pad_zeros(Val(Mid$(DMax("[NMMI_NUM]", "NMPHY"), 2)) + 1, "right", 6)
End If
End Sub
NMMI_NUM is the field, NMPHY is the table
This is in the Module:
Function pad_zeros(number, align, digits)
Dim num As String, s As String
num = Trim$(Str$(number))
If digits = Len(num) Then
s = num
Else
Select Case LCase$(align)
Case "left"
s = num + String$(digits - Len(num), "0")
Case "right"
s = String$(digits - Len(num), "0") + num
End Select
End If
pad_zeros = s
End Function