I have a complex database involving human remains, so the Access forms to input the data are also quite complicated. I have several subforms (Skeleton_index, Element_index, and a few others). The two subforms I mentioned by name send data to their respective fields (element_link, skeleton_link) on an unbound master form, from where the data is used by other forms. For Skeleton_index the user has to input the name of the individual and it shows up in the skeleton_index field instantly. The problem I have is that I don't want the user to have to input the primary key data for Element_index as it should auto_increment. This is a problem because the auto incremented value in Element_id doesn't show up in the element_link field instantly. To get it to show, the user has to create a new element and then go back to the one they were editing. I want to avoid this.
What I want to do is update the Element_id textbox to a new auto_increment primary key when it gets focus. The VBA code should fetch the last primary key from the MySQL (InnoDB) Element_index table, add one to it and then update the value in the Element_id field in the Element_index form.
This is my attempt and it just plain fails.
Private Sub Element_id_GotFocus()
SQL = "SELECT LAST_INSERT_ID();"
lastID = DoCmd.RunSQL(SQL)
newID = Int(lastID) + 1
Element_id.Value = newID
End Sub
EDIT:
The database will have a single user in the first instance, but there may be more in the future.
SOLUTION: I added a button with two macros: one that saves the inserted record and a second one that refreshes the form. No need for complicated VBA.
If Me.Dirty Then Me.Dirty = False
– Minty