Results 1 to 3 of 3
  1. #1
    thefairman is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Location
    California
    Posts
    10

    Adding a record with a new primary key

    Hello. I am a novice to access. I am creating a form to add records to my database. I have created a primary key that is not autonumber. When adding a new record, I want the form to put a primary key default value of the next number in sequence. How do I do this? Thanks in advance.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Requires VBA code. Here is sample from my project:
    Code:
    Public Function GetNewLabNumber() As String
    Dim strLabNum As String
    strLabNum = Nz(DMax("LabNum", "Submit"), "")
    If strLabNum <> "" Then
        If CDate(Left(strLabNum, 4)) = DatePart("yyyy", Date) Then
            GetNewLabNumber = Left(strLabNum, 6) & Format(Right(strLabNum, 4) + 1, "0000")
        Else
            GetNewLabNumber = DatePart("yyyy", Date) & "A-0001"
        End If
    Else
        GetNewLabNumber = DatePart("yyyy", Date) & "A-0001"
    End If
    End Function
    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.

  3. #3
    thefairman is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2011
    Location
    California
    Posts
    10
    Thanks June7. I'm not a programmer, but I have some people in my office that might be able to help me apply your code to my database. Thanks a lot for sharing.

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

Similar Threads

  1. Set Current Record based on Primary Key Input
    By andersonEE in forum Forms
    Replies: 2
    Last Post: 06-24-2011, 08:23 AM
  2. Replies: 2
    Last Post: 03-25-2011, 12:22 PM
  3. Combo boxes both primary keys to select record
    By Alexandre Cote in forum Forms
    Replies: 3
    Last Post: 08-10-2010, 07:11 AM
  4. Replies: 1
    Last Post: 08-03-2010, 01:33 PM
  5. primary key at add new record time
    By elios115 in forum Database Design
    Replies: 1
    Last Post: 06-27-2010, 08:32 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