My FORM has 3 main components along with other components in it.
1. Project Number (Text Box)
2. Created Date (Text Box)
3. Specimen ID (Text Box)
Specimen ID has prespecified format to follow i.e. "YY-Project Number-001" where "YY" is last two digits of the Year pulled from CREATED DATE. "001" is the increamental number which should be populated ON CLICK in the SPECIMEN ID text box. When i click on the next column of the Specimen ID it should populate as "YY-Project Number-002".
I have written the following code at ON CLICK event of the SPECIMEN ID text box: -
Expand|Select|Wrap|Line Numbers
- Private Sub Specimen_ID_Click()
- 'Get the next Spec ID
- Dim NextSpecID As String
- Dim CurrSpecID As String
- On Error Resume Next
- 'Get the last 3 characters from the last specimen ID belonging to this project number
- CurrSpecID = Nz(Right(DMax("[Specimen ID]", "Spec", "[Project Number] = '" & Me.Project_Number & "'"), 3), 0)
- 'Increment the ID by 1
- NextSpecID = Nz(CurrSpecID, 0) + 1
- 'Add in zeros until it's 3 characters long
- Do Until Len(NextSpecID) = 3
- NextSpecID = "0" & NextSpecID
- Loop
- 'Build the next ID.
- NextSpecID = Format(Me.Created_Date, "yy") & "-" & Me.Project_Number & "-" & NextSpecID
- 'Now put it in the form
- Me.[specimen Id] = NextSpecID
- End Sub
This should happen in 1 click.
Kindly help.
Thanks