Results 1 to 2 of 2
  1. #1
    lexygirl is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    8

    Auto generate client number for each new client

    I have a client form which i enter client details on. My primary key is client-id, I have another field which i have called client number, this field gets auto generated each time a client record is saved and it’s this field that i want to view on my form not the client-id (PK).

    I have a table called tblnextnum with field nextnum which generates the next number and gets placed in the client number field.

    I have a command button on my form “SAVE CLIENT” with the following code behind it

    Option Compare Database

    Private Sub Command22_Click()
    On Error GoTo Err_Command22_Click

    DoCmd.GoToRecord , , acNewRec
    Dim NextNo As Long
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT NextNum FROM tblnextnum")
    NextNo = rs!nextnum + 1
    'now update the table
    rs.Edit
    rs!nextnum = NextNo
    rs.Update
    rs.Close
    Set rs = Nothing

    Me.client_number = NextNo

    Exit_Command22_Click:
    Exit Sub

    Err_Command22_Click:
    MsgBox Err.Description


    Resume Exit_Command22_Click

    End Sub


    When I hit the save client button my current client record does not have a client number allocated to it, instead a blank new record appears and that record has the next client number which would have been 8 in the field with all the other fields blank....

    Where should i put this code so that the current record has the next sequential client number allocated to it and it shows up on the form with all the other details i have added in.

    Ideally I would love for the client number to be say 000001, as I need to auto generate invoice number and gift voucher number and would love for it to have leading zeros

    Many thanks for your help

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The new record you see is because that is what the 2nd line (DoCmd.GoToRecord , , acNewRec) is doing. You should add in this number in the BeforeUpdate event of the form. You can *always* display 1 as 000001 by simply using the Format() function.

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

Similar Threads

  1. Auto generate reference number
    By JonB1 in forum Import/Export Data
    Replies: 1
    Last Post: 02-19-2011, 06:38 AM
  2. ODBC Client download
    By indira in forum Import/Export Data
    Replies: 1
    Last Post: 12-25-2010, 03:16 AM
  3. Client Server Database
    By vaikz in forum Database Design
    Replies: 4
    Last Post: 08-02-2010, 04:33 AM
  4. Client Payment History
    By GMatos78 in forum Access
    Replies: 4
    Last Post: 04-24-2010, 09:19 AM
  5. Client History
    By janjan_376 in forum Access
    Replies: 1
    Last Post: 06-23-2009, 02:44 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