Subform retains cursor position from previous page and causes error

Overtime

Registered User.
Local time
Today, 10:43
Joined
Jan 14, 2012
Messages
17
Hello, I am facing a strange problem, I've looked everywhere for a solution but couldn't find any previous posts about it. I will try to explain as best as I can, but it's quite hard to describe.

I have a main form with two fields, and a subform with 5 fields. The main form has ReceiptNumber and ReceiptDate. The subfrom is for Item Details. It's a continuous form, a row for each Item in the Receipt. Pretty standard, you might say.

The purpose of the forms is data-entry. The basic functionality of the forms works perfectly well. The subform is locked by default, I put in code in the main form field's AfterUpdate event to unlock the subform when the main form has been filled. I also put in a code triggered by subform's OnEntry event to check if the main form has been filled, if it was not filled, focus is moved back to main form and subform is locked, this is done to ensure that the subform is re-locked when the user fills out a complete form and moves on to a new main form (ie: a new page.) I am summarizing here, because this is not where the problem occurs. The validation, lock/unlock events work perfectly. I've tested all the data entry scenarios I could think of, going back and forth, filling, deleting, etc. and all was well except for one specific scenario:

When the main form is filled and the user moves on to the subform and fills out a line or two, and then clicks on any field in the middle of the new line (a blank line,) and then tries to move away from that line, Access will show an error telling the user that fieldX is required and was not entered. User would then press "Esc" if he does not want to enter data for a new record. After hitting the escape button, Access would stop giving the user the error message and would allow him to click anywhere he wants. So far this is normal, it is what's supposed to happen, no problem there.

The problem occurs when the user immediately clicks the "new record" button (either on Access's default navigation pane, or on the "add new receipt" button I created.) The main form moves on to a new parent record, both the main form and the subform are cleared as they should. However, the subform, instead of being locked and having one blank line, is unlocked and on focus, and there are two blank lines instead of one. The cursor is still on the field in the middle of the child record, and the same error message ("You must enter a value in fieldX") is displayed again, when the user clicks "OK", my OnEntry event procedure runs and fails (the procedure that's supposed to move focus to the first main form field and lock the subform,) an error message is shown, "Microsoft Access Can't move the focus to the control MainFormField1" and user is given the option to either "End" or "Debug."

The problems seems to be that Access, when moving on a new main form page, never lets go of the cursor's position in the previous page. The cursor is still positioned in the middle of the child record, because of that, it still thinks that the user is trying to create a new child record and, therefor, it is enforcing the entry of the required fields for that table (the child table, AKA the Item Details Table.) This is also causing my procedure to fail, as the procedure is trying to move focus away from the record, and Access, by default, disallows moving away from the empty new record unless the required fields are filled, or the Escape key is pressed.

I tried putting SENDKEYS "{ESC}" on top of my code, but it did nothing. I also tried adding the undo command to cancel incomplete records, but it didn't work either. If it worked, I think it would also delete complete records in normal scenarios, so I didn't think it was a good solution anyway.

This scenario is likely to occur with real users; it could happen this way: The user is entering a list of items from a paper receipt, then, after clicking the next row to enter another item, realized that the last item on the list has already been entered, he would then hit escape and click the "add new receipt button" and then the mess ensues. In fact, the only time this wouldn't happen is:
1- If the user relied only on the TAB key to navigate.
2- or neatly clicked only on the FIRST field on the child record whenever he wanted to add a new item to the list.
3- or if he never changed his mind and never made mistakes.

Needless to say, I really need to solve this problem. This bug is a real nightmare. Like I mentioned, I looked everywhere for information, and I spent at least 14 hours so far trying to find a solution, making changes here and there, but I'm still right where I started. I can't believe that I'm the only one who's facing this problem, as it is an Access-wide issue, rather than an issue specific to my database.

I have no idea what to do next.. Even if I removed my event procedure and figured out another way to control the integrity of the data, there would still be the problem of the user getting an error message requesting the required field.

Is there a way to get access to properly reset the cursor position when moving to a new record in a form?

Any help would be much appreciated.
 
Last edited:
Welcome to the forum.

You could simply put some code in the Main Form's On Current event, that sets the focus to the first filed on the Main Form.
Code:
Me.NameOfFirstFiledOnMainFom.SetFocus
 
Thanks, John :)

I just tried what you suggested, but it didn't work. The focus won't leave the subform in the second page, not even if I tried to click away from it.

I am now trying to write a procedure on the subform's onError event. I thought that maybe I could instruct Access to move focus to first field in the current record if it's empty. That might solve the problem, since the problem only occurs when you leave the first page whilst the cursor is in placed in a field in the middle of the subfrom record.
 
Move the focus to the Main Form before moving to a new record. I would add a Me.Dirty = False before leaving the Sub.

I think you will find that the sub Form is not being saved.

You could open the Table behind the Sub Form to prove this one way or other.
 
Hi Rain, thanks for the response.

How would I move the focus form the main form before moving to a new record? This is what I have been trying to do, but nothing has worked so far. I've tried to set the focus to the main form through the subform's OnExit event, but it doesn't work. It's as if the "required field" error traps the cursor in place unless the user moves it away before hitting the "new record" button.

I don't understand what you mean by adding a Me.Dirty = False before leaving the Sub, would you elaborate?
 
At the beginning of the Code behind the Button for "New Record" place "Me.Dirty=False"

I am assuming the button is on the Sub Form.

A Record is Dirty if something has changed. So Me.Dirty = False is a simple way to save the record.

You could even add this instead

"Me.Dirty=False"
"MsgBox = "AAA"

I expect to see an error because of the code you are using.

If I am correct, then you need to REM all the code that Locks and Unlocks the Sub.

Test and Retest.

Then add one part back at a time Testing constantly.

And a Final word.

BACKUP
BACKUP
BACKUP
BACKUP
BACKUP
 
Hey Rain, thanks for replying.

I finally figured out what was causing this problem, it wasn't what I expected, I was looking in the wrong place. The reason Access was creating two blank lines is because one of the fields in the subform, let's call it FieldB, has an onFocus event that populates another field, FieldC, based on the item that was entered. If no item was entered yet, which is the case when you try to populate the FieldB first, FieldC is set to null. Nothing wrong with there, except that Access apparently considers NULL a proper value, so it adds another blank line because it considers the current blank line already populated.

The problem went away when I changed the procedure that updates FieldC. It used to be:
'query results tested, etc
'if theres something to put in FieldC
FieldC.value = ResultOfCalculation
Else
FieldC.value = Null
End If


Now I just removed the line after Else and kept it empty, the problem went away.

Ugh. I can't believe how much time I wasted on this! I feel really silly...Well,
I'm still new to VBA and I never had any formal training, so I suppose I will run into more and more of these problems, you'll probably be seeing plenty of posts from me.:confused:

Thanks again, I really appreciate your help.
 
That was well done on your behalf.
 
If you learn how to step through your code it will help you solve this type of problem by seeing what is actually happening.
 
You should Google this but basicly;

Open the Code.

Click the gray margin beside a line of executable code.

Notice that a red dot appears in the margin. This breakpoint indicates that execution of the code will pause when it reaches this statement. You can remove the breakpoint by clicking the red dot, but for now leave the breakpoint.

Go back to your Form and Run your command.

It stops at the Breakpoint.

Press F8 to continue one line at a time.

After a line of code that has a Value has been execuated you can pass your cursor over it to view its value.

http://office.microsoft.com/en-us/h...-basic-for-applications-code-HA001042819.aspx
 
Last edited:

Users who are viewing this thread

Back
Top Bottom