Solved

subform to allow only five records

Posted on 2016-09-07
4
51 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
[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
  • 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 50

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 50

Expert Comment

by:Gustav Brock
ID: 41788582
You are welcome!

/gustav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

734 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