Solved

Ms Access Need Help Altering and Combining Code For Subform Limit Record Count and Allow Additions True or False

Posted on 2016-10-26
11
20 Views
Last Modified: 2016-10-26
These are the two codes I have:

Limit Record Count in Contentious Subform:
Private Sub Form_Current()

Dim intMaxNumRecs As Integer
 
    intMaxNumRecs = 1 '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 can't add more than " & intMaxNumRecs & " record of Quantity for each product condition!", vbInformation, "Warning"
                    .MoveLast
                    Me.Bookmark = .Bookmark
                End If
            End If
        End With
    End If
End Sub

Open in new window



And For AllowAdditions:
Private Sub Form_Current()
If Me.RecordsetClone.RecordCount >= 1 Then
Me.AllowAdditions = False
Else
Me.AllowAdditions = True
End If
End Sub

Open in new window



I would like for the code to remove the New record line after the Max Number of Records to Allow. I have tried a few different ways but not working for me. Thanks!
0
Comment
Question by:Dustin Stanley
  • 6
  • 5
11 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41860248
Though only one function is needed, It takes a little more, as you have to call it from several events of the subform - see in-line comments for the usage.
Adjust lngRecordsMax to fit your need:
Public Sub SetFormAllowAdditions( _
  ByVal frm As Form, _
  ByVal lngRecordCountMax As Long)
  
' Limit count of records in (sub)form to that of lngRecordCountMax.
' 2004-10-06, Cactus Data ApS, CPH
'
' Call in (sub)form:
'
'   Private Sub LimitRecords()
'     Const lngRecordsMax As Long = 5
'     Call SetFormAllowAdditions(Me.Form, lngRecordsMax)
'   End Sub
'
'   Private Sub Form_AfterDelConfirm(Status As Integer)
'     Call LimitRecords
'   End Sub
'
'   Private Sub Form_AfterInsert()
'     Call LimitRecords
'   End Sub
'
'   Private Sub Form_Open(Cancel As Integer)
'     Call LimitRecords
'   End Sub

  Dim booAllowAdditions As Boolean
    
  With frm
    booAllowAdditions = (.RecordsetClone.RecordCount < lngRecordCountMax)
    If booAllowAdditions <> .AllowAdditions Then
      .AllowAdditions = booAllowAdditions
    End If
  End With

End Sub

Open in new window

/gustav
0
 

Author Comment

by:Dustin Stanley
ID: 41860275
so would I just drop this into the subform VBA and all the events will take place as needed?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41860314
No, you will save it in a code module and refer to it from your subform as explained in the in-line comments.

/gustav
0
 

Author Comment

by:Dustin Stanley
ID: 41860342
Ok I see. Sorry I was on my mobile at the time and couldn't visualize it until I made the module on my PC. But there is an issue and it is the same for this code as it was doing earlier with my code. If I click the record selectors on the main form the next record subform (If New Record) is blank and I can't add any records to the subform at all not even up to the lngRecordsMax.
0
 

Author Comment

by:Dustin Stanley
ID: 41860367
subform (If New Record)

I take that back. All the records before and after have the New Record Line Gone whether it is new or only has 2 records and the lngRecordsMax is set at 5
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 500 total points
ID: 41860410
You need these two additions:

1. In the parent form:
Private Sub Form_Current()
  
    Call SetFormAllowAdditions(Me!NameOfYourSubformControl.Form, 5)
  
End Sub

Open in new window

2. In the subform:
Private Sub Form_Current()

    Call LimitRecords

End Sub

Open in new window

/gustav
0
 

Author Comment

by:Dustin Stanley
ID: 41860425
Call SetFormAllowAdditions(Me!SkuID.Form, 5)

Open in new window


Returns access object doesn't support this property or method
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41860428
I doubt your subform control is labelled SKuID. Sounds like a field.

/gustav
0
 

Author Comment

by:Dustin Stanley
ID: 41860437
I doubt your subform control is labelled SKuID. Sounds like a field.

Well what do you know it works perfect.
Call SetFormAllowAdditions(Me!sbfrmProducts.Form, 4)

Open in new window

0
 

Author Closing Comment

by:Dustin Stanley
ID: 41860438
Thanks!
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41860440
You are welcome!

/gustav
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now