Solved

subform to allow only five records

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
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.

830 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