coding special autonumber afterinsert() problems

R

rozno

Hi everyone! I am trying to get a special autonumber field to work
after I insert the record into a database that way i can have multiple
users that dont grab the same number when entering a form. So far I
have coded the following afterinsert() function but I am getting a
error ("Run Time Error 3027 Can't update. Database or object is
read-only") at the line where the statement is "rst.AddNew". My
database is not read only and I am the admin on the database so i have
all permissions to edit every object. Could anyone please help me?

Private Sub Form_AfterInsert()

Dim strSQL As String
Dim strTemp As String
Dim strTir As String
Dim RecNum As Long
Dim rst As Recordset

Set dbs = CurrentDb()
strSQL = "SELECT * FROM [TIRMain]"
Set rst = dbs.OpenRecordset(strSQL)

rst.MoveLast
If rst.EOF And rst.BOF Then
RecNum = 1
Else
RecNum = rst("RefNum") + 1
End If

If DatePart("yyyy", Date$) = "2003" Then
strTemp = "S03"
ElseIf DatePart("yyyy", Date$) = "2004" Then
strTemp = "S04"
ElseIf DatePart("yyyy", Date$) = "2005" Then
strTemp = "S05"
Else
strTemp = "S06"
End If

strTir = strTemp & "-" & RecNum

rst.AddNew
rst("RefNum") = RecNum
rst.Update

rst.Close

End Sub
 

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