T
Tony Williams
Arvin I'm using your code you posted in July to create a sequential number
that changes with the year. It nearly does exactly what I want it to do.
The format at the moment is 20030001, 20030002, 20030003 etc I want
12003,22003,32003 etc
I've changed the line
DateNum = Year(Date) & Format(intNumber, "0000") to
Year(Date) & intNumber to get 20031, 20032 but if I change the line to
intNumber &Year(Date) it doesn't work.
What am I missing?
Hope you don't mind this being specifcally for you but I didn't know how
else to contact you.
Thanks in anticipation
Here is all the code you posted
Paste the following code into a module and change the field and table names
to fit your data. You can put it in the form module or in a standard module
(but don't name the standard module with the same name as the function). Use
it in the DefaultValue (property sheet - data tab)of the text box like:
= DateNum()
Function DateNum() As String
'********************************************************************
' Name: DateNum
' Purpose: Generate an incremental "number" based on the year
'
' Author: Arvin Meyer
' Date: July 27, 2003
' Comment: Assumes Table1 As Table and CaseNum As Field
' Generates in the format of 20030001, 20030002, etc.
' Seed the first number in the table if other than 0000
'********************************************************************
On Error GoTo Error_Handler
Dim intNumber As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Select [CaseNum] from [Table1] order by
[CaseNum];")
If Not rs.EOF Then
rs.MoveLast
If Left(rs.Fields("CaseNum"), 4) = CStr(Year(Date)) Then
intNumber = Val(Mid(rs.Fields("CaseNum"), 5)) + 1
Else
intNumber = 1
End If
End If
DateNum = Year(Date) & Format(intNumber, "0000")
With rs
.AddNew
!CaseNum = DateNum
.Update
End With
Exit_Here:
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function
Error_Handler: 'If someone is editing this record trap the error
Dim intRetry As Integer
If Err = 3188 Then
intRetry = intRetry + 1
If intRetry < 100 Then
Resume
Else 'Time out retries
MsgBox Err.Number, vbOKOnly, "Another user editing this number"
Resume Exit_Here
End If
Else 'Handle other errors
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Problem
Generating Number"
Resume Exit_Here
End If
End Function
Tony Williams
that changes with the year. It nearly does exactly what I want it to do.
The format at the moment is 20030001, 20030002, 20030003 etc I want
12003,22003,32003 etc
I've changed the line
DateNum = Year(Date) & Format(intNumber, "0000") to
Year(Date) & intNumber to get 20031, 20032 but if I change the line to
intNumber &Year(Date) it doesn't work.
What am I missing?
Hope you don't mind this being specifcally for you but I didn't know how
else to contact you.
Thanks in anticipation
Here is all the code you posted
Paste the following code into a module and change the field and table names
to fit your data. You can put it in the form module or in a standard module
(but don't name the standard module with the same name as the function). Use
it in the DefaultValue (property sheet - data tab)of the text box like:
= DateNum()
Function DateNum() As String
'********************************************************************
' Name: DateNum
' Purpose: Generate an incremental "number" based on the year
'
' Author: Arvin Meyer
' Date: July 27, 2003
' Comment: Assumes Table1 As Table and CaseNum As Field
' Generates in the format of 20030001, 20030002, etc.
' Seed the first number in the table if other than 0000
'********************************************************************
On Error GoTo Error_Handler
Dim intNumber As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Select [CaseNum] from [Table1] order by
[CaseNum];")
If Not rs.EOF Then
rs.MoveLast
If Left(rs.Fields("CaseNum"), 4) = CStr(Year(Date)) Then
intNumber = Val(Mid(rs.Fields("CaseNum"), 5)) + 1
Else
intNumber = 1
End If
End If
DateNum = Year(Date) & Format(intNumber, "0000")
With rs
.AddNew
!CaseNum = DateNum
.Update
End With
Exit_Here:
rs.Close
Set rs = Nothing
Set db = Nothing
Exit Function
Error_Handler: 'If someone is editing this record trap the error
Dim intRetry As Integer
If Err = 3188 Then
intRetry = intRetry + 1
If intRetry < 100 Then
Resume
Else 'Time out retries
MsgBox Err.Number, vbOKOnly, "Another user editing this number"
Resume Exit_Here
End If
Else 'Handle other errors
MsgBox Err.Number & ": " & Err.Description, vbOKOnly, "Problem
Generating Number"
Resume Exit_Here
End If
End Function
Tony Williams