Cycle all records (1 Viewer)

John Sh

Member
Local time
Tomorrow, 06:35
Joined
Feb 8, 2021
Messages
416
I am using single forms set to cycle through all records. This is not a problem, it's actually very handy, until I come to the last record, after which I get a new record.
Where should I add code to detect the new record and return to the first record after the leaving the last record? Should I add it to the form's current event or the form's last tabbed control?
I suppose I'm asking at what point does Access create the new record?
Obviously this detection would need to be such that I can still create a new record when required.
 

bob fitz

AWF VIP
Local time
Today, 21:35
Joined
May 23, 2011
Messages
4,726
Try your code in the OnCurrent event. Perhaps:
Code:
Private Sub Form_Current()
    If Me.NewRecord Then
        DoCmd.GoToRecord , , acFirst
    End If
End Sub
But won't you ever need to add a new record?
EDIT:
Sorry, I missed the last sentence of post #1 until my reply had been posted
 

John Sh

Member
Local time
Tomorrow, 06:35
Joined
Feb 8, 2021
Messages
416
EDIT:
Sorry, I missed the last sentence of post #1 until my reply had been posted
Thanks Bob.
That last line is the kicker.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:35
Joined
May 7, 2009
Messages
19,246
add a button (btnNewRecord) to add new record:
Code:
Dim bolCanAdd As Boolean
Dim lngRec As Long

Private Sub btnNewRecord_Click()
    
    lngRec = CurrentRecord
    bolCanAdd = True
    DoCmd.GoToRecord , , acNewRec
    
End Sub

Private Sub Form_AfterUpdate()

    bolCanAdd = False
    
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

    Cancel = Not bolCanAdd
    If Cancel Then
        Me.TimerInterval = 30
    End If
    
End Sub

Private Sub Form_Current()
    
    If Me.NewRecord Then
        If Not bolCanAdd Then
            DoCmd.GoToRecord , , acFirst
        End If
    Else
        bolCanAdd = False
    End If
    
End Sub

Private Sub Form_Timer()
    Me.TimerInterval = 0
    If lngRec <> 0 Then
        DoCmd.GoToRecord , , acGoTo, lngRec
    End If
    lngRec = 0
End Sub

Private Sub Form_Undo(Cancel As Integer)

    bolCanAdd = False
    Me.TimerInterval = 30
    
End Sub
 

John Sh

Member
Local time
Tomorrow, 06:35
Joined
Feb 8, 2021
Messages
416
I already have an "Addnewrecord " button that has a fair bit of housekeeping to do, so that's in place.
I wondered if the rest of your code is another way of saying "allowadditions = false"?
So, in the current event I put:

if not me.newrecord then
me.allowadditions = false
end if

Then in the addnewrecord button I have me.allowadditions = true just proir to the docmd.gotorecord, , acnewrec

Problem solved.

Once again I thank you for your input.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:35
Joined
May 7, 2009
Messages
19,246
if your Table has "no record" yet, setting AllowAdditions to False, youll
get a blank form without any clue of what fields whatsoever:
blank_record.png


if you already have, you will never get to "Newrecord" when you browse your table.
 

John Sh

Member
Local time
Tomorrow, 06:35
Joined
Feb 8, 2021
Messages
416
if your Table has "no record" yet, setting AllowAdditions to False, youll
get a blank form without any clue of what fields whatsoever:
Most of my tables have multiple hundreds of records.
The problem showed up with a newly constructed table that had only one record.
I was able to add a new record via the button but records were automatically added when I scrolled passed the last field/record.
The code I added in the current event has stopped the unwanted addition and by reversing the allowadditions to true in the new record button I only get a new record when that button is clicked, so the best of both worlds.
The database is split so the tables are never viewed in datasheet form unless by myself whilst developing and then only a copy.
John
 

Attachments

  • jelly-bean-day-fun1.jpg
    jelly-bean-day-fun1.jpg
    45.3 KB · Views: 42

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:35
Joined
May 7, 2009
Messages
19,246
Most of my tables have multiple hundreds of records.
of course they do have records, but if you want to "re-use" your db starts from "no records", you'll get a form just
what i shared.
the Snapshot form is a Single form not a datasheet.
records were automatically added when I scrolled passed the last field/record.
then you may have some code that explicitly put some values to any field when you go to new record.
that is why it is saving, even when you think you did not put anything to any field.
this sometimes occurs when you have default values set.

anyway goodluck.
 

Users who are viewing this thread

Top Bottom