Results 1 to 5 of 5
  1. #1
    iProRyan is offline Advanced Beginner
    Windows 7 64bit Access 2003
    Join Date
    Dec 2011
    Location
    England, United Kingdom
    Posts
    44

    *NOT SOLVED* Default Values = Number of Records + 1

    Hi Guys,

    I have a bit of a problem with a Form in Access, what I want is a fields Default Value to be the Total Number of Records for that form + 1. I don't want to use an AutoNumber because that messed with Relationships. If anyone can help, it would be much appreciated.



    Ryan

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The problem here is that a form does not have set number of records - its underlying table does. The form will display all or only some of them, depending on filtering that is set.

    I gather from what you said that when you insert a new record you want to have a field be assigned the value one higher than the current maximum, i.e. the next number is a sequence.

    You can do this using VBA in the Before Insert event of your form, with the DMax function:

    me![ID_Field] = nz(dmax("ID_Field","TableName"),0) +1

    This finds the current maximum value of ID_Field, adds 1 to it, and puts the resulting value in the form field.

    I used nz to cover the case where there are no existing records in the table.

    One caveat with this is that if you create this new record, and then delete it, the next new record you create will have the same ID number. There are also potential problems if you have multiple users accessing the same table at the same time.

    HTH

    John

  3. #3
    iProRyan is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Location
    England, United Kingdom
    Posts
    44
    Quote Originally Posted by John_G View Post
    The problem here is that a form does not have set number of records - its underlying table does. The form will display all or only some of them, depending on filtering that is set.

    I gather from what you said that when you insert a new record you want to have a field be assigned the value one higher than the current maximum, i.e. the next number is a sequence.

    You can do this using VBA in the Before Insert event of your form, with the DMax function:

    me![ID_Field] = nz(dmax("ID_Field","TableName"),0) +1

    This finds the current maximum value of ID_Field, adds 1 to it, and puts the resulting value in the form field.

    I used nz to cover the case where there are no existing records in the table.

    One caveat with this is that if you create this new record, and then delete it, the next new record you create will have the same ID number. There are also potential problems if you have multiple users accessing the same table at the same time.

    HTH

    John
    Hi John,

    I have entered this into the Before Insert event of the form

    Me![DVD ID] = Nz(DMax("DVD ID", "DVD's"), 0) + 1

    and get this error:

    Syntax Error (missing operator) in query expression.

    Any ideas?

    Ryan

  4. #4
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The problem is the Tablename "DVD's". The single quote (apostrophe) in it is confusing MS Access. Rename the table to remove the single quote and it should work. It is never a good idea to use special characters in MS Access object names (other than the underscore "_"), and this is one reason why.

    John

  5. #5
    iProRyan is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2011
    Location
    England, United Kingdom
    Posts
    44
    Thanks managed to get it to work, I think it was also the space in "DVD ID" causing an issue because I wouldn't work until I removed the space. Thank you so much for your help John!

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

Similar Threads

  1. Replies: 6
    Last Post: 07-25-2011, 01:54 PM
  2. Default number format?
    By joewilly1 in forum Queries
    Replies: 1
    Last Post: 11-24-2010, 09:57 AM
  3. Replies: 1
    Last Post: 03-25-2009, 02:20 PM
  4. Replies: 1
    Last Post: 01-08-2007, 01:21 PM
  5. Default Values
    By twainscott in forum Programming
    Replies: 0
    Last Post: 09-14-2006, 11:18 AM

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