I found this example somewhere and use it to save my main form and have two more subs similar to this to save two subforms in case the user wants to undo the data. It was working at one point but now the bookmark is always zerolength as it would be for a new record even when I open the form with data in it. I call this sub from the Form OnCurrent event. Any ideas? TIA bwilkens
Sub SaveMain(f As Form, TempTable As String)
Dim DB As Database: Set DB = CurrentDb()
Dim FormRS As DAO.Recordset, TempRS As DAO.Recordset
Dim i As Integer
On Error GoTo Err_SaveMain
' Clear temp table
DB.Execute "Delete * From [" & TempTable & "];"
' Open temp table
Set TempRS = DB.OpenRecordset(TempTable, DB_OPEN_TABLE)
If Err Then
Beep: MsgBox "BUG: " & Error, 48
Exit Sub
End If
' Get Mainform recordset
Set FormRS = f.RecordsetClone
' Find current record in mainform recordset
On Error Resume Next
If f.Bookmark = "" Then Exit Sub
FormRS.Bookmark = f.Bookmark
' If error, we must be at a new record. Exit, since nothing to save.
If Err Then Exit Sub
'
' Create a copy of the main form record in the temp table
'
On Error GoTo Err_SaveMain
' Create a new record
TempRS.AddNew
' Loop through the fields in the new temp table
' .. record, copying into each the mainform field value.
For i = 0 To TempRS.Fields.Count - 1
TempRS(i) = FormRS(TempRS(i).Name)
Next i
' Save the new record
TempRS.Update
Bye_SaveMain:
Exit Sub
Err_SaveMain:
Beep
MsgBox "BUG: " & Error, 48
Resume Bye_SaveMain
End Sub
Sub SaveMain(f As Form, TempTable As String)
Dim DB As Database: Set DB = CurrentDb()
Dim FormRS As DAO.Recordset, TempRS As DAO.Recordset
Dim i As Integer
On Error GoTo Err_SaveMain
' Clear temp table
DB.Execute "Delete * From [" & TempTable & "];"
' Open temp table
Set TempRS = DB.OpenRecordset(TempTable, DB_OPEN_TABLE)
If Err Then
Beep: MsgBox "BUG: " & Error, 48
Exit Sub
End If
' Get Mainform recordset
Set FormRS = f.RecordsetClone
' Find current record in mainform recordset
On Error Resume Next
If f.Bookmark = "" Then Exit Sub
FormRS.Bookmark = f.Bookmark
' If error, we must be at a new record. Exit, since nothing to save.
If Err Then Exit Sub
'
' Create a copy of the main form record in the temp table
'
On Error GoTo Err_SaveMain
' Create a new record
TempRS.AddNew
' Loop through the fields in the new temp table
' .. record, copying into each the mainform field value.
For i = 0 To TempRS.Fields.Count - 1
TempRS(i) = FormRS(TempRS(i).Name)
Next i
' Save the new record
TempRS.Update
Bye_SaveMain:
Exit Sub
Err_SaveMain:
Beep
MsgBox "BUG: " & Error, 48
Resume Bye_SaveMain
End Sub