I have a continous form on which the .AllowAdditions=False
. There is a button that will add a new record by using the following code:
Dim bkm As Variant
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("tbl_Unit", , dbAppendOnly)
With rs
.AddNew
.Fields("UnitID") = DMax("UnitID", "tbl_Unit") + 1
.Fields("ObjektID") = 0
.Fields("UnitTypID") = 0
.Update
bkm = .Bookmark
End With
Me.Requery
Me.Recordset.Bookmark = bkm
End Sub
The record is added as expected, but the bookmark method of jumping to the newly added record in the form, does not work. I get the run time error 3159: Not a valid bookmark. As far as I can tell, I am following the same logic that Microsoft provides in their documentation page here. The error occurs on line Me.Recordset.Bookmark = bkm
Why am I getting the error?
A bookmark belongs to a recordset, not a table. So try:
Dim UnitId As Long
Dim rs As DAO.Recordset
UnitId = DMax("UnitID", "tbl_Unit") + 1
Set rs = CurrentDb.OpenRecordset("tbl_Unit", , dbAppendOnly)
With rs
.AddNew
.Fields("UnitID") = UnitId
.Fields("ObjektID") = 0
.Fields("UnitTypID") = 0
.Update
.Close
End With
Me.Requery
Set rs = Me.RecordsetClone
rs.FindFirst "UnitID = " & UnitId & ""
If Not rs.NoMatch Then
Me.Bookmark = rs.Bookmark
End If
rs.Close