Results 1 to 5 of 5
  1. #1
    shockerty is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    4

    Project number increment

    Hi all,



    I am setting up a database which includes a table of clients (tblClient) and a table of projects (tblProjects).

    tblClients has a field called ClientID which is an integer.
    tblProjects has a field called Client that looks up the ClientID from tblClients, linking the project to a client. There is also a field called ProjectID which is also an integer.

    In a form designed to input new project info, I am looking for a way to autofill the project number with the next integer in the series once the Client has been chosen from the lookup combo box (called lstClient).

    I have attempted some VBA to do this but it just gives 1 as the project number every time.

    Code:
    Private Sub ProjectID_AfterUpdate()
    
    Dim HighestClientProject As Integer
    
    HighestClientProject = DMax("ProjectID", "tblProject", "Client=" & lstClient)
    
    Me = HighestClientProject + 1
    
    
    End Sub
    Effectively this is the kind of client & project number progression I would like to achieve:

    Client / Project

    1/1
    1/2
    2/1
    2/2
    2/3
    2/4
    3/1
    3/2

    I hope that makes sense. I am a VBA noob. Can anyone give me a pointer in the right direction?

  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    Try this.
    Nz(DMax("ProjectID", "tblProject") + 1)
    HTH

  3. #3
    AlexHedley's Avatar
    AlexHedley is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    England
    Posts
    180

    Project number increment


  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Could try setting DefaulValue property then no VBA needed.

    DMax("ProjectID", "tblProject", "Client=" & lstClient)

    Is this a multi-user db?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Code:
    Private Sub ProjectID_AfterUpdate()
    
      Dim HighestClientProject As Integer
    
      HighestClientProject = DMax("ProjectID", "tblProject", "Client=" & lstClient)
    
      Me = HighestClientProject + 1
    
    
    End Sub

    First off, you're trying to populate the ProjectID Control by using the AfterUpdate event of ProjectID! ProjectID_AfterUpdate only fires after data is entered into the ProjectID Control! The code should be in the AfterUpdate event of the Control where the Client is selected; if I'm reading this correctly, that would be the Listbox named lstClient.

    Secondly, to assign the generated ID, you're using the line

    Me = HighestClientProject + 1

    This needs to be

    Me.ProjectID = HighestClientProject + 1

    so, if this is a single user, free standing database, you can use

    Code:
    Private Sub lstClient_AfterUpdate()
    
      Dim HighestClientProject As Integer
    
      HighestClientProject = DMax("ProjectID", "tblProject", "Client=" & lstClient)
    
      Me.ProjectID = HighestClientProject + 1
    
    End Sub

    If, on the other hand, this is a multi-user database, the code must go in the Form_BeforeUpdate event, instead, so that two users, entering a New Record at the same time, won't end up with the same ProjectID for both. Using the DefaultValue Property, on a multi-user database, would present the same problem, as two users could start New Records, simultaneously, and the same ID would be assigned, because the Default Value is assigned when a Record is first started, not at the last moment prior to it being saved.

    Linq ;0)>

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

Similar Threads

  1. Increment part of a serial number
    By swavemeisterg in forum Forms
    Replies: 10
    Last Post: 04-15-2013, 01:59 PM
  2. Replies: 3
    Last Post: 02-01-2013, 07:09 PM
  3. increment number in first column
    By learning_graccess in forum Queries
    Replies: 7
    Last Post: 04-03-2012, 02:56 PM
  4. Replies: 5
    Last Post: 11-12-2010, 12:10 PM
  5. How to Increment A Number Field
    By Advanced in forum Programming
    Replies: 3
    Last Post: 01-27-2010, 02:36 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