Solved

subform to allow only five records

Posted on 2016-09-07
4
26 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

743 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

11 Experts available now in Live!

Get 1:1 Help Now