I have a situation where I allow users to filter data on a single form. If they choose to add a new record, if the data entered in the new record does not match the filter criteria, I want to remove the filter and then go to the newly created record.
I added code to the AfterInsert event procedure to use the RecordsetClone and Bookmark combination, but when the procedure ends, it does not display the correct record. As I step through the code, it seems to work just fine – at least right up until the End Sub. Then the record displayed changes.
Here is an example: My recordset for the form includes 50 records. When a filter is applied, the underlying recordset changes and the record count is reduced to 5 records. If I add a record matching the filter criteria, everything is good and now the record count is 6 records.
However, if the record being added does not match the filter criteria, the code in the AfterInsert procedure saves the key of the record just added, updates the form’s RecordSource to remove the filter, clones the recordset, finds the record just added, and bookmarks it. The recordset now has 51 records and the newly created record happens to be the 45th record. However, when the routine ends, the 6th record is displayed (where the new record was before the RecordSource changed to remove the filter).
Here is a snippet of the code from the AfterInsert routine after a check against the filter criteria:
How can I get this to work right?
I added code to the AfterInsert event procedure to use the RecordsetClone and Bookmark combination, but when the procedure ends, it does not display the correct record. As I step through the code, it seems to work just fine – at least right up until the End Sub. Then the record displayed changes.
Here is an example: My recordset for the form includes 50 records. When a filter is applied, the underlying recordset changes and the record count is reduced to 5 records. If I add a record matching the filter criteria, everything is good and now the record count is 6 records.
However, if the record being added does not match the filter criteria, the code in the AfterInsert procedure saves the key of the record just added, updates the form’s RecordSource to remove the filter, clones the recordset, finds the record just added, and bookmarks it. The recordset now has 51 records and the newly created record happens to be the 45th record. However, when the routine ends, the 6th record is displayed (where the new record was before the RecordSource changed to remove the filter).
Here is a snippet of the code from the AfterInsert routine after a check against the filter criteria:
Code:
[SIZE=3][FONT=Calibri] myProject = Me.Project_Name[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Me.RecordSource = "SELECT tbl_Project.* FROM tbl_Project ORDER BY tbl_Project.[Project Name];"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Set rs = Me.RecordsetClone[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] With rs[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] .FindFirst "[Project Name]='" & myProject & "'"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] If .NoMatch Then[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] MsgBox "The new record could not be found. Please contact the database administrator.", vbCritical, "Critical Error"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] GoTo Exit_Form_AfterInsert[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Else[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Me.Bookmark = .Bookmark[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] End If[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] .Close[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] End With[/FONT][/SIZE]
How can I get this to work right?