Solved Dont allow mor than 9 records (1 Viewer)

oxicottin

Learning by pecking away....
Local time
Today, 03:06
Joined
Jun 26, 2007
Messages
851
Hello, I have a button to add records and I need to limit the number to 9. In my code below its bypassing the check and going to the adding of records? That's wrong with code?

Code:
   Dim intMaxNumRecs As Integer
    intMaxNumRecs = 9 'Max Number of Records to Allow
    If Me.NewRecord Then
        With Me.RecordsetClone
            If .RecordCount > 0 Then
                .MoveLast:  .MoveFirst
                If .RecordCount >= intMaxNumRecs Then
                MsgBox "You cant have more than 9 SubForm Entries"
                 End If
            End If
        End With
      Exit Sub
      Else
    Me.AllowAdditions = True
    DoCmd.GoToRecord , , acNewRec
    txtSequentialNumber = strNxtNum
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord , , acLast
    Me.AllowAdditions = False
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 03:06
Joined
May 21, 2018
Messages
8,463
Your exit sub appears to be in the wrong place and I would think it comes after the msgbox. However, the fact it runs tells me that it is never a new record.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:06
Joined
Oct 29, 2018
Messages
21,358
Have you tried stepping through the code? Just curious...
 

oxicottin

Learning by pecking away....
Local time
Today, 03:06
Joined
Jun 26, 2007
Messages
851
I got it working with:

Code:
Dim intMaxNumRecs As Integer
    intMaxNumRecs = 9 'Max Number of Records to Allow
  
            If Me.Recordset.RecordCount >= intMaxNumRecs Then
                MsgBox "You cant have more than 9 SubForm Entries"
                  Exit Sub
               End If
 
    Me.AllowAdditions = True
    DoCmd.GoToRecord , , acNewRec
    txtSequentialNumber = strNxtNum
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord , , acLast
    Me.AllowAdditions = False
 

theDBguy

I’m here to help
Staff member
Local time
Today, 00:06
Joined
Oct 29, 2018
Messages
21,358
I got it working with:

Code:
Dim intMaxNumRecs As Integer
    intMaxNumRecs = 9 'Max Number of Records to Allow
 
            If Me.Recordset.RecordCount >= intMaxNumRecs Then
                MsgBox "You cant have more than 9 SubForm Entries"
                  Exit Sub
               End If

    Me.AllowAdditions = True
    DoCmd.GoToRecord , , acNewRec
    txtSequentialNumber = strNxtNum
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord , , acLast
    Me.AllowAdditions = False
Good job!
 

Users who are viewing this thread

Top Bottom