Solved

subform to allow only five records

Posted on 2016-09-07
4
44 Views
Last Modified: 2016-09-07
Access 2010 vba:

I'm trying to limit the numbers of records to enter into a subform.
The forms main "current event".
I have:
With Me![dbo_t_redbook_pricing_escalation_detail_subform].Form
  If .Recordset.RecordCount <= 5 Then
    .AllowAdditions = True
    .AllowEdits = True
  Else
    .AllowAdditions = False
    .AllowEdits = True
  End If
End With

Open in new window


But I never get prompted to stop any data entry beyond 5 records ?

Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
  • 2
4 Comments
 
LVL 3

Author Comment

by:fordraiders
ID: 41788072
I found this bit of code and it works great.
Public Function LimitRecords( _
          frm As Access.Form, _
          Optional RecLimit As Integer = 1)
 
  ' Limit the number of records in the form passed as
  ' to no more than the number specified by .
 In a module:
  With frm.RecordsetClone
    If .RecordCount <> 0 Then .MoveLast
    frm.AllowAdditions = (.RecordCount < RecLimit)
  End With
End Function

Open in new window


Called by

LimitRecords Me, 5

But Just need some stop message when trying to enter a 6th record. Or Some Alert.
Thanks
fordraiders
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41788324
I have posted that before:
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

You can extend the last part:

  With frm
    booAllowAdditions = (.RecordsetClone.RecordCount < lngRecordCountMax)
    If booAllowAdditions = False Then
        MsgBox "No more records allowed."
    End If
    If booAllowAdditions <> .AllowAdditions Then
      .AllowAdditions = booAllowAdditions
    End If
  End With

Open in new window

/gustav
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 41788376
Thanx very much !
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41788582
You are welcome!

/gustav
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

832 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