Solved

limit max records

Posted on 2006-07-11
6
1,162 Views
Last Modified: 2008-02-01
Hi

Have a form (continuous) based on single table.
How do I limit max number of records that can be inserted into this table. Want it to hold max 5 records.

Thank you.
0
Comment
Question by:ohgee
6 Comments
 
LVL 34

Expert Comment

by:jefftwilley
ID: 17087311
How are you inserting? You can loop through an insert SQL statement to accomplish what you want if that's how you're doing it. If it's an insert query go to properties and select Top5. Any other criteria?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17087384
Hi ohgee,

Here is one way.  Use this code for your form's BeforeInsert event:


Private Sub Form_BeforeInsert(Cancel As Integer)

    Dim cnn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
   
    Const RecLimit As Long = 7 'max records allowed in table
   
    Set cnn = CurrentProject.Connection
    rs.ActiveConnection = cnn
   
    rs.Open "SELECT Count(*) AS NumRecs FROM YourTable"  'update as needed
   
    If rs!NumRecs >= RecLimit Then
        MsgBox "Cannot add more records without violating limit"
        Cancel = True
    End If
   
    Set rs = Nothing
    Set cnn = Nothing
   
End Sub


Regards,

Patrick
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17087442
ohgee,
> How do I limit max number of records that can be inserted into this table. Want it to hold max 5 records.

Please keep in mind that my suggestion should keep people from entering new records *using the form* once the
limit's been reached (or exceeded).

However, this will not prevent someone from just opening the table and directly entering new records.

Regards,

Patrick
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:ohgee
ID: 17087486
Thanks Patrick, form limitation is sufficient.
Will check it out tomorrow evening.

A top value might work too, form is based on a query. But am more interested in preventing data entry, rather than allowing unlimited entry but only showing 5
Thanks
0
 
LVL 54

Accepted Solution

by:
nico5038 earned 75 total points
ID: 17088109
In a case like this I use in the OnCurrent:

Private Sub Form_Current()
If Me.RecordsetClone.RecordCount >= 5 Then
   Me.AllowAdditions = False
Else
   Me.AllowAdditions = True
End If
End Sub

This stops the possibility to add rows.

Nic;o)
0
 

Author Comment

by:ohgee
ID: 17089429
Will use Nico's solution, works fine as is.
Thanks for all the help.
Regards
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

860 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