Results 1 to 15 of 15
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237

    fixed number of records in a table

    Hi,
    Is it possible to have a fixed number of records in a table?


    The user has a form to enter records and they should have only 9 records. How can this be done?

    Thanks
    Khalil

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    always better to show examples, your post can be interpreted as

    'table can only have 9 records'
    'table can only have 9 records per user'
    'user can only enter up to 9 records at a time'
    'user has to enter 9 records at a time'

    assuming it is the first per your header, you can't stop them entering directly into a table, but programatically you might use something like

    Code:
    if dcount("*","myTable")>9 then
        msgbox "Yo can only have 9 records"
        cancel=true
    end if
    in your form before update event

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Thank you for the response
    I used the code on the form before update event but did not work.
    I have a front end and a back end database. It allowed me to add record number 10

    Khalil

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    try =9 instead

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by Ajax View Post
    you can't stop them entering directly into a table
    They could by using data macros. I know they're not popular though.

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    You can use a routine as below. But I don't believe there is a way to see what check constraints
    exist on tables in Access. So if you do use such routines, you should document same.
    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: limit_NumRows
    ' Purpose: Add constraint to table to limit the number of records allowed
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Parameter tblInvolved (String): table to get the constraiint
    ' Parameter AllowMax (Integer): N=Max number of records allowed
    ' Author: Jack 'from researching forums
    ' Date: 20-Aug-18
    ' ----------------------------------------------------------------
    Public Sub limit_NumRows(tblInvolved As String, AllowMax As Integer)
        Dim strSql As String
        strSql = "ALTER TABLE " & tblInvolved _
           & " ADD CONSTRAINT MaxRecs" _
           & " CHECK ((SELECT Count(*) FROM " & tblInvolved & ") <=" & AllowMax & ");"
        Debug.Print strSql
        MsgBox strSql
        CurrentProject.Connection.Execute strSql
    End Sub

  8. #8
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    I have this code working:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
       
       Dim intNumberOfRecords As Integer
       intNumberOfRecords = DCount("*", "tblInstitutions")
       Debug.Print intNumberOfRecords
    If intNumberOfRecords > 8 Then
        MsgBox "Yo can only have 8 records"
        Cancel = True
    End If
    
    
    End Sub
    Thanks to all of you
    Khalil

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Could be as simple/short as
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.Recordset.Recordcount = 8 Then MsgBox "You can only have 8 records."
    End Sub
    I can understand having a max record count for the many side of a relationship but not for a whole table.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Why not just set the AllowAdditions property of the form to False on load if 9 records exist?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Suggest we need the real requirement from Khalil.
    Does he want a message to indicate his logical max records has been reached? But user could add more.
    Does he want to prevent physical additions once his max records has been reached?

  12. #12
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Thank again for all the replies.

    I want to prevent physical additions once max records has been reached with a message indicating that the max number of records has ben reached.

    Khalil

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Another method:

    Code:
    Private Sub Form_Current()
        If Me.Recordset.RecordCount >= 9 Then
            Me.AllowAdditions = False
            MsgBox "No more records can be added"
        End If
          
    End Sub
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Isladogs, in a form with navigation controls, you'll get that message for every record you land on that has 9 records.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Good point!
    Perhaps something like the Form_BeforeUpdate event instead? Not tested!
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 08-13-2019, 05:25 PM
  2. export fixed to txt with number column align right
    By Tickland in forum Import/Export Data
    Replies: 3
    Last Post: 10-13-2017, 06:13 AM
  3. Replies: 2
    Last Post: 08-12-2016, 07:00 AM
  4. Generate Fixed Number of Entries
    By dustonabt12 in forum Reports
    Replies: 6
    Last Post: 07-10-2013, 01:55 PM
  5. creation of reports with fixed number of rows
    By georgia in forum Reports
    Replies: 1
    Last Post: 02-17-2010, 10:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums