Hi all,
I have a problem with something that I thought would be very simple and trivial.
I have a table with three fields: an Autonumber field, a date field and a text field. I also have a form with a command button to add a new record. When I add the record for the first time it works fine. If/when I try add another record(s) it gives me run-time error 3022 (no duplicates allowed).
Here is my code:
Other words, I am trying to add a new record and get its autonumber ID.
When I open my table I can only see one line. However if I delete that line and add another "first" record I can see that the Autonumber field is actually changed, so my code is working (just not the way I want it to...
).
Any help or advice will be appreciated.
Regards,
Michael
I have a problem with something that I thought would be very simple and trivial.
I have a table with three fields: an Autonumber field, a date field and a text field. I also have a form with a command button to add a new record. When I add the record for the first time it works fine. If/when I try add another record(s) it gives me run-time error 3022 (no duplicates allowed).
Here is my code:
Code:
Private Sub cmdGetInMod_Click()
Dim intModID As Integer
Dim strTable as string
strTable = "tbl_TEST"
intModID = getInModID(strTable)
end Sub
Private Function getInModID(strTable As String) As Integer
Dim tempDB As DAO.Database
Dim tempRST As DAO.Recordset
Dim strSQL As String
Dim strUsername As String
Dim strDate As String
strUsername = Environ("username")
strDate = Date
strSQL = "SELECT * FROM " & strTable & ";"
Set tempDB = CurrentDb()
Set tempRST = tempDB.OpenRecordset(strSQL, dbOpenDynaset)
With tempRST
.AddNew
.Fields(1).Value = strDate
.Fields(2).Value = strUsername
.Update '<= this is what gives error message
.bookmark = .LastModified
getInModID = .Fields(0).Value
.Close
End With
Set tempRST = Nothing
Set tempDB = Nothing
End Function
Other words, I am trying to add a new record and get its autonumber ID.
When I open my table I can only see one line. However if I delete that line and add another "first" record I can see that the Autonumber field is actually changed, so my code is working (just not the way I want it to...

Any help or advice will be appreciated.

Regards,
Michael