VBA Access – Empty Recordset (error 3021) in Form.Current event

I am attempting to perform some actions based on the current value of fields in the Form.Recordset in the Form.Current event.

An example would be If Me.RecordSet("LastUpdate") < "#1 Jan 2020#" Then MsgBox "Last year's Comment"

Me.RecordSet could be something like "SELECT ID, LastUpdate, Comment FROM Comment WHERE ID = 12" or it could also return several records, e.g. "SELECT ID, LastUpdate, Comment FROM Comment WHERE ID < 12"

I get the Error 3021: No current record on the above line. It appears that although the Form.Current event has launched and the Me.Recordset Property correctly gives the underlying SQL, the recordset has not been populated yet?

If in the immediate pane when when debugging this line I do Me.Recordset.MoveFirst it then works. But of course, resetting the current record to the first record every time the Form.Current event fires is not the way forward…

It seems strange to me that in the Form.Current event, the Form.Recordset has not yet been populated? Is this right? Any thoughts on the correct way to do what I am trying to do?

The underlying tables are on a MySql server, although that should be irrelevant?

Edit: It appears that the Form.Current event fires 3 times… and only the last time triggers this error. So ignoring the error could be viable, but clearly I’d like to understand why that event is fired 3 times… I will need to look at the calling code.

Still not sure I understand how/why Form.Recordset would become invalidated.

Add Comment
2 Answer(s)

Either you use the field link directly from the form

Me!LastUpdate 

Or use the linked control (if control is bound to the field)

Me.BoundControl 

Or if you need to use the recordset then you need to use the recordset clone and synchronize it to the form

Me.RecordsetClone.Bookmark = Me.Bookmark Me.RecordsetClone.Fields("LastUpdate") = ... 
Answered on July 16, 2020.
Add Comment

Don’t use Me.RecordSet("...") to retrieve values. You mainly need this to set the RecordSet property of the form: https://docs.microsoft.com/en-us/office/vba/api/access.form.recordset

To get values of the current record, use

Me!LastUpdate.Value     ' for the control Me("LastUpdate").Value  ' same Me.LastUpdate.Value     ' for the bound column 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.