Results 1 to 14 of 14
  1. #1
    Lynn is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    25

    Auto increment when the form opens

    Hi,
    I have a form with a subform. I don't want to use autonumber for my [ReferralNumber] field because I don't want the records to start at 1. But I would like to have the field autopopulated with a number each time I open a new form. So if I decided to have the referral number begin at 100 and increment by 1 with each new form that opens how would that look?
    Thanks,


    Lynn

  2. #2
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    Reading your thread I assume you have the following requirement:

    you have a Main form with a subform you want a field on the main form to autoincrement from 100 (Not AutoNumber) and the Subform will be linked to this field.

    This is What I have done:

    1) I have two tables increment and sub.
    2) Main form increment is opened in dataentry mode.
    3) RegID is incremented from 100 onwards.
    4) Subform link field = RegID Main form.

    Code used:

    On the OnLoad Even of the Increment Form:

    intIncrementalNumber = IIf(IsNull(DMax("[RegID]", "increment")), 100, DMax("[RegID]", "increment") + 1)
    Me.RegID = intIncrementalNumber

    How to check:

    When ever you open the incremental form the RegID is incremented.

    if this solves your problem mark this thread solved.

  3. #3
    Lynn is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    25
    Thanks Maximus, looks great...I can't seem to open the forms in your zip file. Something about a run time error.
    Lynn

  4. #4
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    I was able to open it fine (3rd party input ftw). Are you sure you extracted it first?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can always start an autonumber field at your given number if you want. That assumes you can live with the autonumber's limitations. The DMax method is better if you can't have any skipped numbers.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Lynn is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    25
    Hi,
    Still couldn't your form--run time error keeps showing up but got it to work on my main form. Thing is, I'd like for it to only increment on new blank forms. Each time I open an existing record, it increments. Sorry, should have mentioned that earlier.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you stay with that method, switch to the current event and include this test:

    If Me.NewRecord Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    the trick is to import the datavase objects on to another blank access database. Glad you could make it work. As Sir Paul pointed out it you want the numbers to increment for a new record use


    If Me.NewRecord Then
    Followed by the code.


    I had opened the main form in dataentry mode and had no navigation buttons so it work on the onload event. i am re-attaching the mdb again.

  9. #9
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    mark the thread solved.

  10. #10
    Datagopherdan is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Dec 2008
    Posts
    220
    Just something to keep in mind with this setup. You can end up with alot of blank records if you have a new record being inserted each time the form is open unless you have some type of procedure to prevent that. Just food for thought. Good luck.

    Dan
    Access Development

  11. #11
    Lynn is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    25

    Autoincrement

    Quote Originally Posted by pbaldy View Post
    You can always start an autonumber field at your given number if you want. That assumes you can live with the autonumber's limitations. The DMax method is better if you can't have any skipped numbers.
    Hi Paul,
    Can you tell how to set this up? I'm not worried about skipped numbers and the DMax method is just not working like I want it to. I added a switchboard and autoexec to open a blank record and it won't increment and I'd eventually like to add a command button that adds a new record and I'm sure it wouldn't increment with that either. The autonumber method seems much simpler.
    Thanks,
    Lynn

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Once your table is set up, append a record into it, populating the autonumber field with the number previous to the one you want it to start with (99 for instance). Then delete that record, but don't compact/repair the database. The next record added to the table will have an autonumber of 100.

    INSERT INTO TableName (FieldName)
    VALUES (99)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    This is how I can make an autonumber Field start from any number I want.
    Suppose I have a Table1 with a autonumber Field aa. I want to start the number sequence to begin from no 100. Follow the steps carefully.

    assuming that you are just starting to build the database.

    Note: unjoin all relationship with the table on which you will carry out this exercise on.

    Note: If your autonumber field in the original table is a primary key or any primary key, you must temporarily remove the primary key before running the append query. Also, if your original table contains fields that have the Required property set to Yes, the Indexed property set to Yes (No Duplicates), or field and/or record ValidationRule property settings that prevent Null entries in fields, you must temporarily disable these settings

    1) copy Table1 structure only name it Table2.
    2) Change the the autonumber Field aa to Number (set its FieldSize property to Long Integer ) in Table 2.
    3) Make a Dummy entry in Table2 aa=99
    4) Now make a append query. In the append to option select Table1. Select Table 2 from the table list and append aa of table 2 to aa of table 1.
    5) Open Table 1 and Delete the dummy entry appended in it. Now when you start data entry you autonumber begins from 100.

    This will alter the autonumber sequence and make it start from 100.

    if you have data in the table make two copies only the structure. Follow the steps. after autonumber is adjusted. append the data from the original table into the adjusted table. remember while appending data append all fields other than the autonumber field. Delete original table. rename the new table with adjusted autonumber as the deleted table. Set up relationships and other properties that you had disabled.

    remember if you want to compact and repair the database do it after you have entered at least one entry in the new adjusted table.


    I was rather surprised that you could not get the DMax method going. From the last thread I assume that you have put up a swithchboard, if you are opening the form the Dmax code could have been put the code onto the onload event of the form. in this case the Form dataentry property has to be set to yes to ensure that a blank form opens all the time. Remember if the form open with all the entries it will modify the Field of the first entry of the form. This can be also set to the onCurrent even of the form like Sir Paul pointed out but with the if condition

    If Me.NewRecord Then

    in this case you may open the form to show all entries as number in the field will only be incremented if a new record is opened.


    Then the same code could have been added to the new button on the form.

    anyways guess I have wasted enough time on this thread

  14. #14
    Lynn is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2010
    Posts
    25
    Paul, Thanks much for your help.

    Maximus, I apologize if my questions are too simple for you. Clearly your expertise is being "wasted" on my inquiries. I hope I misunderstood your comment.

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

Similar Threads

  1. How to Increment A Number Field
    By Advanced in forum Programming
    Replies: 3
    Last Post: 01-27-2010, 02:36 PM
  2. Form opens differently from control button
    By bdaniel in forum Programming
    Replies: 5
    Last Post: 12-29-2009, 12:38 PM
  3. Replies: 2
    Last Post: 12-08-2009, 01:19 PM
  4. Shortcut that ONLY opens Access Form
    By uneek78 in forum Forms
    Replies: 0
    Last Post: 03-30-2009, 06:52 AM
  5. Auto-increment for non-100% numerical fields
    By supernoob in forum Access
    Replies: 0
    Last Post: 05-03-2007, 09:44 PM

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