Reputation:
As in the title I cam across a strange phenomenon. I have a form which contains two subforms. On both subforms I have a button which triggers requery of the relevant subform. If after loading the form, I immediately click on this button (requery the form) I get error "3021: No current record" when I try to save the value of the primary key of the current record in the OnCurrent event to a variable. Strangely enough in the debugger the relevant values are like this:
Form.CurrentRecord=1
Form.RecordSet.Absoluteposition=-1
Form.RecordSet.RecordCount=14
Form.RecordSet.EOF=False
Form.RecordSet.BOF=False
Also note, that when the form is loaded, in the load event it still works correctly and the there I can save the primary key value which is the primary key contained in the first record.
Somewhere between Form_Load and me clicking that requery button the status of the form is getting out of synch. I just recently switched from Access 2003 to 2007 and as far as I can remember this error did not happen before (although I might just not click this button right afer load).
For now I have a workaround, but I'd really like to understand how this can happen.
Upvotes: 3
Views: 7361
Reputation: 20731
I have the exact same problem when I try to go to "Design View". I don't need to click quickly, I can wait an hour and then do that and bang! there is this error.
Note that I only get the error if the sub-form is still empty (i.e. really does not include any records which is the case when I first open the window.) So I always thought that was normal.
The AbsolutePosition is set to -1 when the cursor is not currently pointing to a specific position. Also, this value is zero based (as mentioned by someone else: first row is AbsolutePosition 0). However, this position may be -1 even if you have focus on a specific row in the sub-form. This means it is useless.
What you want to use if you need to know the current cursor position is CurrentRecord. This is a number that starts at 1. I would imagine that if the list is empty, the CurrentRecord may be set to 0 or -1 representing the fact that no row is available.
I used all of these in a function used to calculate a complex total of different columns, here is the page http://linux.m2osw.com/msaccess-sum-focus-recordset-problem
Upvotes: 0
Reputation: 57023
If this is ADO, check which OLE DB provider is being used e.g. (guessing)
Debug.Print Form.RecordSet.ActiveConnection.Provider
If by chance it is the 3.51 version, see:
INFO: AbsolutePosition Property with JET Databases in ADO
...if you are using the ACE provider, perhaps it's a regression bug?!
Upvotes: 1
Reputation: 23067
You have to place the record pointer on a particular record in the form's Recordset via a MoveFirst or whatever. And the return value appears to be zero-based instead of 1-based.
I can't reproduce the error you're getting returning -1. Are you setting the form's recordset to an ADO recordset? If so, that might explain it -- DAO recordsets never return -1 for any of these values, but I believe that before a .MoveLast an ADO RecordCount returns -1. Perhaps AbsolutePosition does the same in ADO.
What are you trying to accomplish? I don't see any utility in using the form's Recordset object when it's so much easier to assign a recordsource and navigate the RecordsetClone (which is much more closely bound to the form's edit/display buffer than the form's Recordset, particularly when the Recordset has been assigned with an ADO recordset).
Upvotes: 0