G
Guest
I'm developing a rather simple database but I want to give a unique
identifier to each "employee" and each "case". When entering a "new"
employee in the database, it doesn't show the autonumber field. So, I put a
field in and tried to write a module that loops through the database and
returns the next higher number. It's not working... help?
Here's what I wrote:
Function FindMax(empID)
Dim db As DAO.Database
Dim mx As Integer
Dim rs As DAO.Recordset
Dim rsVal As String
Set db = CurrentDb (employee_database)
Set rs = db.OpenRecordset ("increment", dbOpenDynaset)
rs.MoveFirst
rsVal = rs.Fields("[Employee_ID_number]").Value
' Set mx equal to the numeric portion of the field.
mx = Right(rsVal, Len(rsVal) - 3)
'Loop to make sure you have the maximum number.
Do While Not rs.EOF
rsVal = rs.Fields("[Employee_ID_number]").Value
If Richt(rsVal, Len(rsVal) - 3) > mx Then
mx = Right(rsVal, Len(rsVal) - 3)
End If
rs.MoveNext
Loop
'Increment the maxium value by one and
'combine the text with the maxium number.
FindMax = "EM-" & (mx + 1)
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function
identifier to each "employee" and each "case". When entering a "new"
employee in the database, it doesn't show the autonumber field. So, I put a
field in and tried to write a module that loops through the database and
returns the next higher number. It's not working... help?
Here's what I wrote:
Function FindMax(empID)
Dim db As DAO.Database
Dim mx As Integer
Dim rs As DAO.Recordset
Dim rsVal As String
Set db = CurrentDb (employee_database)
Set rs = db.OpenRecordset ("increment", dbOpenDynaset)
rs.MoveFirst
rsVal = rs.Fields("[Employee_ID_number]").Value
' Set mx equal to the numeric portion of the field.
mx = Right(rsVal, Len(rsVal) - 3)
'Loop to make sure you have the maximum number.
Do While Not rs.EOF
rsVal = rs.Fields("[Employee_ID_number]").Value
If Richt(rsVal, Len(rsVal) - 3) > mx Then
mx = Right(rsVal, Len(rsVal) - 3)
End If
rs.MoveNext
Loop
'Increment the maxium value by one and
'combine the text with the maxium number.
FindMax = "EM-" & (mx + 1)
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Function