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
29 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 50

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 50

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 
LVL 50

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 50

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 50

Expert Comment

by:Gustav Brock
ID: 41860440
You are welcome!

/gustav
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

726 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