This is what I have done:
I have a simple table Table1 with the Following Fields:
- ID (Text Field PK)
- T_Date (Date/Time) This is to record the trasaction Date
- Details
The Code below is attached to a form bound to Table1.
What Does the Code Do?
Generates the ID automatically in the format mentioned e.g. N0418-1, N0418-2
How does it work?
I have used a simple Select Case to assign Alpha value to the current year. I use a simple recordset to see if I have any entries for the current date this is done using:
if rst.EOF and rst.Bof
if no record is found the then it becomes the first entry e.g. if for today 04/19/2012 first entry N0419-01. If record is found I use do while loop to loop through the records and find the number of records and so my new record will be i+1. e.g. I have 5 Entries for 04/19/2012 then the next entry will be N0419-6
I have attached this code to a command button on my form please use your discretion.
Code:
Private Sub Command9_Click()
Dim rst As DAO.Recordset
Dim strSQL As String
Dim i As Integer
Dim intCurYear As Integer
Dim strYearCode As String
i = 0
intCurrentYear = Year(Date)
Select Case intCurrentYear
Case Is = 2011
strYearCode = "M"
Case Is = 2012
strYearCode = "N"
End Select
MsgBox strYearCode
strSQL = "Select * From Table1 Where T_Date=#" & Me.T_Date & "#;"
Set rst = CurrentDb.OpenRecordset(strSQL)
If rst.EOF And rst.BOF Then
Me.ID = strYearCode & IIf(Month(Date) <= 9, "0" & Month(Date), Month(Date)) & IIf(Day(Date) <= 9, "0" & Day(Date), Day(Date)) & "-" & 1
Else
Do While Not rst.EOF
i = i + 1
rst.MoveNext
Loop
End If
rst.Close
Set rst = Nothing
Me.ID = strYearCode & IIf(Month(Date) <= 9, "0" & Month(Date), Month(Date)) & IIf(Day(Date) <= 9, "0" & Day(Date), Day(Date)) & "-" & i + 1
End Sub
let me know if you have problems.