Results 1 to 6 of 6
  1. #1
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151

    Need help with VBA code for a simple function

    I have a number field (not an autonumber) that I want to increment by 1 when the user creates a new record. I keep having trouble with vba for this.

    I have written pseudo code in comments, but can't seem to find the correct way to do it in VBA.



    Private Sub cmdNewbtn_Click()

    Dim rst As Recordset
    Dim newrecnum As Long
    Dim tempnum As Long

    'Find last record (table = tblCongregationTeams)

    'Store TeamID + 1 to tempnum

    'Find new record


    'Store tempnum to tblCongregationTeams.TeamID



    End Sub


    Thanks!!!

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    use what is commonly called 'dmax+1'

    tempnum=dmax("TeamID","tblCongregationTeams")+1

    If your app is a multi user app then this must be done in the form beforeupdate event otherwise there is a risk of two users getting the same number if doing this at the same time

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I would not call it TeamID either? ID is normally associated with Autonumber. TeamNumber perhaps?
    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

  4. #4
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151
    Thanks, CJ London.
    I also need vba syntax for these:

    'Find last record (table = tblCongregationTeams)

    'Store TeamID + 1 to tempnum

    'Find new record

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    As others have already stated, you can use the Dax() function to get the largest value and then add 1 to it.
    There are hundreds of pages on the required syntax, for example: https://support.microsoft.com/en-us/...b-c554053d299f
    Give it a go and post back with your code if you get stuck.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Finding the "Last" record is very subjective. The "Last" record is dependent on the sort order.
    The "Last" record if sorted (ordered) by "Last Name" will be different if the sort order is on a date and will be different if sorted by the (autonumber) PK field.
    Note that a table has NO inherent order. It is a "bit bucket".

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

Similar Threads

  1. Replies: 17
    Last Post: 03-26-2022, 10:31 AM
  2. Simple SETVALUE function
    By fishhead in forum Access
    Replies: 2
    Last Post: 07-21-2020, 05:53 PM
  3. Replies: 4
    Last Post: 05-25-2014, 02:01 AM
  4. a simple getter function for a constant
    By veloopity in forum Programming
    Replies: 10
    Last Post: 10-19-2013, 12:17 PM
  5. Need Help with Simple VB Function
    By nrage21 in forum Programming
    Replies: 2
    Last Post: 09-21-2010, 09:14 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