A
Aaron S via AccessMonster.com
Hey guys.. trying to get a custom "auto" numbering system going here... First
off, YES I've googled it many times, and found many different ways to do it...
but so far, none of them are working right for what I need... I need a
simple yy-00000 format number for each record in a set of tables.... I've got
4 different types of reports users can choose from, to store different types
of data, but need all 4 to be numbered off a central sequence... (Aka: User
could create report A with ID # 05-00001, but if they tried to add a report B
it would be # 05-00002 rather than a different 05-00001 even though it's a
different type of report)
The way I've got it set up at the moment, I have a form that opens to give
users the choice of which type of report they would like to create, via 4
command buttons... in the BeforeUpdate part of this choice form, I have
coding from Jeff Polack that he posted a while back, to determine & assign
the next sequenced # based on a table I have created solely to store the
current #... when a user clicks one of the 4 buttons to open the actual
report form, i would like the new report # stored back into that table, AND
copied into the new report they're entering... Copying it is easy, I have
hidden fields on the 'choice' form that the actual report forms refer to for
values.... the problem is, this code isn't assigning anything into the hidden
fields based off of the table, and it doesn't appear to be updating the table
at all either, as it's still blank... I'll post my code here to see if that
helps anyone solve this for me...
OH AND before I forget... I'm a novice at this.... Been learning the VBA
aspect of things as I go here... I understand most of what Jeff's coding here
does, but I'm not seeing what I'm doing wrong....
Please please please help!
Private Sub Form_BeforeInsert(Cancel As Integer)
RepNum = NextCustomCounter("Current#")
End Sub
Public Function NextCustomCounter(TableName As String) As String
On Error GoTo NextCustomCounterErr
Dim rs As DAO.Recordset
Dim RepNum As String
Dim NextCounter As String
Dim intCount As Integer
Set rs = CurrentDb.OpenRecordset("Current#", 2)
'Open the DAO recordset.
With rs
.FindFirst "TableName = '" & TableName & "'"
If Not .NoMatch Then 'When the table is found,
If Not IsNull(!RepNum) Then 'And a counter entry exists
'Check to see if it's a new year
If Left(!RepNum, 2) = Format(Date, "yy") Then
'Use the next sequental counter
RepNum = !RepNum
Else
'If this is the first transaction of the new year, reset
the counter
RepNum = Format(Date, "yy") & "-0"
End If
Else
RepNum = Format(Date, "yy") & "-0"
End If 'Counter exists
'Update the record with the next entry
.Edit
Else
'Add the new table & counter to the CustomCounters table
RepNum = Format(Date, "yy") & "-0"
.AddNew
!TableName = TableName
End If
'Increments by one the value of the numeric portion of the counter
'Determines the length of the string by subtracting 3 (YY-)
intCount = Val(Right(RepNum, (Len(RepNum) - 3))) + 1
'Formats the updated value of the counter
NextCounter = Format(Date, "yy") & "-" & CStr(intCount)
'Store the counter
!RepNum = NextCounter
.Update
End With 'recordset
'Clear the recordset
Set rs = Nothing
'Sets for return to the calling code
NextCustomCounter = NextCounter
Exit Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'The following error routine should be replaced with a custom
'error routine.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
NextCustomCounterErr:
MsgBox "Error " & Err & ": " & Error$
'If Err <> 0 Then Resume
'End
End Function
off, YES I've googled it many times, and found many different ways to do it...
but so far, none of them are working right for what I need... I need a
simple yy-00000 format number for each record in a set of tables.... I've got
4 different types of reports users can choose from, to store different types
of data, but need all 4 to be numbered off a central sequence... (Aka: User
could create report A with ID # 05-00001, but if they tried to add a report B
it would be # 05-00002 rather than a different 05-00001 even though it's a
different type of report)
The way I've got it set up at the moment, I have a form that opens to give
users the choice of which type of report they would like to create, via 4
command buttons... in the BeforeUpdate part of this choice form, I have
coding from Jeff Polack that he posted a while back, to determine & assign
the next sequenced # based on a table I have created solely to store the
current #... when a user clicks one of the 4 buttons to open the actual
report form, i would like the new report # stored back into that table, AND
copied into the new report they're entering... Copying it is easy, I have
hidden fields on the 'choice' form that the actual report forms refer to for
values.... the problem is, this code isn't assigning anything into the hidden
fields based off of the table, and it doesn't appear to be updating the table
at all either, as it's still blank... I'll post my code here to see if that
helps anyone solve this for me...
OH AND before I forget... I'm a novice at this.... Been learning the VBA
aspect of things as I go here... I understand most of what Jeff's coding here
does, but I'm not seeing what I'm doing wrong....
Please please please help!

Private Sub Form_BeforeInsert(Cancel As Integer)
RepNum = NextCustomCounter("Current#")
End Sub
Public Function NextCustomCounter(TableName As String) As String
On Error GoTo NextCustomCounterErr
Dim rs As DAO.Recordset
Dim RepNum As String
Dim NextCounter As String
Dim intCount As Integer
Set rs = CurrentDb.OpenRecordset("Current#", 2)
'Open the DAO recordset.
With rs
.FindFirst "TableName = '" & TableName & "'"
If Not .NoMatch Then 'When the table is found,
If Not IsNull(!RepNum) Then 'And a counter entry exists
'Check to see if it's a new year
If Left(!RepNum, 2) = Format(Date, "yy") Then
'Use the next sequental counter
RepNum = !RepNum
Else
'If this is the first transaction of the new year, reset
the counter
RepNum = Format(Date, "yy") & "-0"
End If
Else
RepNum = Format(Date, "yy") & "-0"
End If 'Counter exists
'Update the record with the next entry
.Edit
Else
'Add the new table & counter to the CustomCounters table
RepNum = Format(Date, "yy") & "-0"
.AddNew
!TableName = TableName
End If
'Increments by one the value of the numeric portion of the counter
'Determines the length of the string by subtracting 3 (YY-)
intCount = Val(Right(RepNum, (Len(RepNum) - 3))) + 1
'Formats the updated value of the counter
NextCounter = Format(Date, "yy") & "-" & CStr(intCount)
'Store the counter
!RepNum = NextCounter
.Update
End With 'recordset
'Clear the recordset
Set rs = Nothing
'Sets for return to the calling code
NextCustomCounter = NextCounter
Exit Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'The following error routine should be replaced with a custom
'error routine.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
NextCustomCounterErr:
MsgBox "Error " & Err & ": " & Error$
'If Err <> 0 Then Resume
'End
End Function