Next record button has started creating new records (1 Viewer)

wackywoo105

Registered User.
Local time
Today, 02:46
Joined
Mar 14, 2014
Messages
203
I've used this code for years to move to the next record. Over the last week or so it has started adding a new record if you are on the last record. It has never done this before in probably about 10 years. Can anyone help with how to stop it creating new blank records?

Code:
On Error GoTo Err_Command322_Click

    DoCmd.GoToRecord , , acNext

Exit_Command322_Click:
    Exit Sub

Err_Command322_Click:
    MsgBox Err.Description
    Resume Exit_Command322_Click
 

wackywoo105

Registered User.
Local time
Today, 02:46
Joined
Mar 14, 2014
Messages
203
Thanks. I found and disabled "allow additions". The problem is now the add new record button won't work. Both this and next record button produce a pop up of "You can't go to the specified record", which is what use to happen with the next record button when you got to the end.

EDIT:

I now have:

Code:
On Error GoTo Err_Command327_Click

Me.AllowAdditions = True
    DoCmd.GoToRecord , , acNewRec
Me.AllowAdditions = False

Exit_Command327_Click:
    Exit Sub

Err_Command327_Click:
    MsgBox Err.Description
    Resume Exit_Command327_Click
   
End Sub

and

Code:
On Error GoTo Err_Command322_Click

Me.AllowAdditions = False
    DoCmd.GoToRecord , , acNext
Me.AllowAdditions = True

Exit_Command322_Click:
    Exit Sub

Err_Command322_Click:
    MsgBox Err.Description
    Resume Exit_Command322_Click

I've never needed this before. I wish they would stop changing and breaking things that have worked flawlessly for years.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:46
Joined
May 21, 2018
Messages
8,529
Over the last week or so it has started adding a new record if you are on the last record. It has never done this before in probably about 10 years.
Did you add any code to the form that could force a record to save?
For example did you add
me.dirty = false
in one of the events?
 

LarryE

Active member
Local time
Today, 02:46
Joined
Aug 18, 2021
Messages
591
I doubt it is actually adding a new record. It is probably just going to a new one so it can be added. Just add
DoCmd.GoToRecord , , acLast
to your error routine, so on error it goes to the last record, not a new one.
 

wackywoo105

Registered User.
Local time
Today, 02:46
Joined
Mar 14, 2014
Messages
203
Hmm. I have a back up copy and testing this there are no new records created. There are no extra me.dirty = false. Allow additions in form properties is true. I can't see why it has changed how it works. I think something must be entering data and thus activating the new form. I've done that much recently I'm not sure what but I guess I will find it at some point.

Either way I prefer the new version, as now definitely can't accidently create a new record.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:46
Joined
Feb 19, 2002
Messages
43,275
There are no extra me.dirty = false.
Doesn't matter. If your code dirties the record, Access will save the record whether you tell it to or not. There is almost certainly new code in the module that is causing the problem
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:46
Joined
Sep 12, 2006
Messages
15,656
Maybe your current event has some code that would set a value in the new record. I'm not sure, but would any default values be set immediately on entering the new record area?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:46
Joined
Feb 19, 2002
Messages
43,275
Defaults defined at the table level get set when the data gets saved. I found this out the hard way:( Defaults defined at the form level get set when the form gets dirtied. Defaults, by themselves, do not dirty a form.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:46
Joined
May 21, 2018
Messages
8,529
I found this out the hard way:(
Can you explain what are the different impacts of these two methods? In what cases do you need to be concerned of these impacts. My initial assumption would have been that there would not be any impactful difference since nothing is saved until record is saved.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:46
Joined
Feb 19, 2002
Messages
43,275
I've only run into one situation where this caused me a problem and that is when a value in a field that had a default was checked in the BeforeUpdate event so it was before the record was saved and depending on the value of the field with the default, something different happened. So, in this case, since the BE was SQL Server, the default wasn't applied in the form. I think when the BE is ACE, the default gets applied as soon as the record gets dirty as it does for form defined defaults. So, the solution was to modify the form to add the default for this particular control. The default remained on the table column, not that it mattered.
 

wackywoo105

Registered User.
Local time
Today, 02:46
Joined
Mar 14, 2014
Messages
203
Thanks everyone. I think I have altered something to update/save data on a new form. I will track it down when I have time, but for now allowadditions = false is keeping it at bay.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:46
Joined
Feb 19, 2002
Messages
43,275
Did you at least write yourself a note and leave it prominently displayed in the code? Unless this is an emergency release, using bandaids and just moving on is poor practice. You should fix the code while the problem is fresh in your minds.

Unsolicited advice from someone with the scars to prove that good practice matters and you need to make it a habit.
 
Local time
Today, 05:46
Joined
Feb 28, 2023
Messages
628
Semi-old thread, but I used navigation buttons from Stephen Lebans and found it was doing the same thing and modified it slightly.
I can't find the link, it is similar to this: https://www.access-programmers.co.uk/forums/threads/custom-navigation-button.164220/ - and someone else modified it and I found the link recently but apparently didn't save it.

Found: https://www.lebans.com/recnavbuttons.htm

The next button code looks like this:
Code:
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

'With Me.Parent
  If Me.Parent.CurrentRecord < Me.Parent.Recordset.RecordCount Then
    Me.Parent.Recordset.MoveNext
'  Else - Removed MB 24-Aug-2023
'    .Recordset.AddNew
  End If
'End With

Exit_cmdNext_Click:
    Exit Sub

Err_cmdNext_Click:
    Box Err.DESCRIPTION
    Resume Exit_cmdNext_Click
    
End Sub

The complicated part was having the button "grayed out" when you were on the last record, but I got that working also, eventually.
 

LarryE

Active member
Local time
Today, 02:46
Joined
Aug 18, 2021
Messages
591
You can set the forms cycle property to Current Record, that way you must click a button to add a new record and there is no need to alter the forms Allow Additions property. The buttons On Click Event triggers:
DoCmd.GoToRecord acActiveDataObject, , acNewRec
but you need to actually enter something before the record can be saved. If you are concerned about unauthorized records, this may help.
 
Local time
Today, 05:46
Joined
Feb 28, 2023
Messages
628
Try LarryE's suggestion. Was news to me, but it makes sense.
Both this and next record button produce a pop up of "You can't go to the specified record", which is what use to happen with the next record button when you got to the end.
Would not happen with the code in Reply #15

Your Add Record Button Code was:
Code:
On Error GoTo Err_Command327_Click

Me.AllowAdditions = True
    DoCmd.GoToRecord , , acNewRec
Me.AllowAdditions = False

Exit_Command327_Click:
    Exit Sub

Err_Command327_Click:
    MsgBox Err.Description
    Resume Exit_Command327_Click
 
End Sub
Two things here:

If you set AllowAdditions = False in the form properties, the code above is correct, BUT ... You need to move the Me.AllowAdditions = False line to the Form_AfterInsert() Event. Then the add button will work again. (I did the same thing when I was trying to prevent unauthorized additions and got the same error and this solved it - from another forum thread on here).

If you set AllowAdditions = True in the form properties, you shouldn't need either Me.AllowAdditions statement for this button.

Your revised Next Record code was:
Code:
On Error GoTo Err_Command322_Click

Me.AllowAdditions = False
    DoCmd.GoToRecord , , acNext
Me.AllowAdditions = True

Exit_Command322_Click:
    Exit Sub

Err_Command322_Click:
    MsgBox Err.Description
    Resume Exit_Command322_Click
I think you were just trying things here to see what would work, but ...

If AllowAdditions is false in the form properties, you don't need either of the Me.AllowAdditions statements above. It is already false and you DO NOT want to set it to True. If AllowAdditions = True in the form properties, then this could work, but as you said, you will get the "You can't go to the specified record" message if you are on the last record, which would annoy me.

Finally - I'm assuming that you have the built-in Form Navigation buttons hidden if you are adding code to your own buttons. Otherwise, the built-in buttons may still add a new record - I think LarryE's code may fix that.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:46
Joined
Feb 19, 2002
Messages
43,275
Code:
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click

'With Me.Parent
  If Me.Parent.CurrentRecord < Me.Parent.Recordset.RecordCount Then
    Me.Parent.Recordset.MoveNext
'  Else - Removed MB 24-Aug-2023
'    .Recordset.AddNew
  End If
'End With

Exit_cmdNext_Click:
    Exit Sub

Err_cmdNext_Click:
    Box Err.DESCRIPTION
    Resume Exit_cmdNext_Click
   
End Sub
The reason this code causes empty records is because you are moving to the next record, which is empty and then moving one more so the first empty record gets saved. You have NO validation code that prevents the empty record from being saved. While setting the form's cycle property to current record seems to solve the problem, it doesn't actually. You can still save empty records, just not this way. The better solution is to use the form's beforeUpdate event to keep an empty record from being saved.

PS, ALWAYS give your controls meaningful names. Command322 is just sloppy and also an indication of how many controls you have added to this form.
 
Local time
Today, 05:46
Joined
Feb 28, 2023
Messages
628
@Pat Hartman posted my code, which does NOT create empty records, instead of the OP's.

Commend322 does indicate at least the minimum number of controls added to the form, but not necessarily the number of controls currently on the form, but I agree, meaningful names would be helpful.
 

Isaac

Lifelong Learner
Local time
Today, 02:46
Joined
Mar 14, 2017
Messages
8,777
If your code dirties the record, Access will save the record whether you tell it to or not

That's one of the things I dislike about Access. It is just a bit too save-happy, which isn't necessarily "right" or "wrong", it's just wildly different from what most users are used to when using most other platforms, which don't risk eventually finalizing a saved record just because you started filling out the first 3 letters of your name! Access is great, I'm just saying this is one of my few complaints.
 
Local time
Today, 05:46
Joined
Feb 28, 2023
Messages
628
Concur with @Isaac. I've worked around it, but just about any other application let's you make changes and then when you want to exit asks "Do you want to Save?" (which is annoying if you just saved and haven't made additional changes.)

Unless you add some kind of Before_Update verification, Access saves and then hopes you have a backup of the old version if you didn't intend to make the change.
 

Users who are viewing this thread

Top Bottom