Results 1 to 8 of 8
  1. #1
    A kinason is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    23

    Creating an incremental field not an "autonumber field"

    I have a small database which is use for my small school. On my admission form, each student is assigned a "StudentID" which is of this format (VIP001, VIP002, VIP003 etc).



    The problem is that at any time i have to type in the student Id and i have discovered that this causes ommissions like entering VIP012, when the next students comes, the last ID is forgotten and one might enter VIP014.

    Is ther a way of having the next student id appear on the form automatically when the current entry is validated?

    I will be vey grateful receiving help from you.

  2. #2
    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 Form_BeforeUpdate(Cancel As Integer)
    
    If Me.NewRecord Then
     
      If RecordsetClone.RecordCount = 0 Then
         Me.StudentID = "VIP001"
      Else
         Me.StudentID = "VIP" & Format(DMax("Mid([StudentID],4)", "tblStudents") + 1, "000")
      End If
    
    End If
    
    End Sub

    Replace tblStudents with the actual name of the Table that holds the data. Ditto with StudentID, unless that is the actual Field Name.

    Being in the Form_BeforeUpdate, the number will increment at the last possible second before the Record is saved. This sounds like a single-user app, from your description, and so it doesn't really matter, but in a multi-user app you want the number to be assigned at the very last moment, to decrease the possibility of two users getting the same number; it's probably good to get in the habit of doing it like this.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    A kinason is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    23
    i have entered the code but when i validate, it stores only VIP in the StudentId field of the table Student.

  4. #4
    A kinason is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    23
    i have entered the code in the AfterUpdate Property of the form but when i validate, it stores only VIP in the StudentId field of the table Student.

  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
    Perhaps you should try putting it in the Form_BeforeUpdate event, as you were shown, in the example!

    Also, remember to replace tblStudents, in the code, with the actual name of your Table that holds this data.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    A kinason is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    23
    HERE IS THE CODE I INSERTED IN THE BEFORE UPDATE EVENT OF THE FORM. THE TABLE NAME IS "Student" AND THE StudentId is in column 2 of this table

    Private Sub Form_BeforeUpdate(Cancel As Integer)

    If Me.NewRecord Then

    If RecordsetClone.RecordCount = 0 Then
    Me.StudentId = "VIP001"
    Else
    Me.StudentId = "VIP" & Format(DMax("Mid([StudentId],4)", "Student") + 1, "000")
    End If

    End If

    End Sub
    End Sub

  7. #7
    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
    And...what happened?

    First off, you need to remove the second Sub End statement. With that in place, I don't understand how your code ran at all, without an error being popped.

    Then go into the Table and either delete the Records that only showed 'VIP" in the StudentID or edit them to show proper data, and trying running the code again. This is tested code, and shouldn't be a problem.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    A kinason is offline Novice
    Windows XP Access 2007
    Join Date
    May 2013
    Posts
    23
    Thanks Missinglinq its working. Am so happy

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

Similar Threads

  1. Replies: 3
    Last Post: 04-22-2013, 06:08 AM
  2. Replies: 1
    Last Post: 10-08-2012, 09:01 AM
  3. Replies: 2
    Last Post: 09-29-2012, 11:22 PM
  4. Replies: 1
    Last Post: 03-03-2012, 10:17 PM
  5. Replies: 16
    Last Post: 07-22-2011, 09:23 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