Auto Number with data entry

C

Chhunhour

1).I have a problem with AutoNumber on my database with ID
field, example if I have ID like: E001,E002,E003,E004,E005
then if I delete a record with ID E003 then the data store
in my table will become as follow: E001,E002,E004,E005 and
when a form loard AutoNumber will start ID from E006 so
how should I do to make it start among the missing ID,E003?

Below is my coding of AutoNumber:

Function AutoID() As String
Dim id%
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("select ID from
tblemployee order by ID")
If rst.RecordCount > 0 Then
rst.MoveLast
id = Right(rst(0), Len(rst(0)) - 1)
id = id + 1
AutoID = "E" & Format(id, "000")
Else
AutoID = "E001"
End If
rst.Close
End Function

Please help correct the above code to my requirement as
possible.

2).Please help show code printing a document or report on
any kind of printer?


Thanks,
Chhunhour.
 
U

user

Instead of going to the last record, trimming the number off, adding one,
and continuing, you'd have to loop through the sorted recordset checking if
the current ID is one less than the next ID. If not then use the ID plus
one, otherwise carry on looping. If you get to the end of the recordset,
there are no gaps so just use the last ID plus one.

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top