Format Questions with auto-generate new records

Mojo1

Board Regular
Joined
Mar 6, 2003
Messages
148
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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,223,533
Messages
6,172,886
Members
452,486
Latest member
standw01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top