Automatically incrementing in Access

Hi. I'm using Ms Access 2000. I have a table in which the data type of the primary key is a 'number'. Please tell me how can I automatically increment the value so that when I add a new record it automatically gives the record a unique value.

Thanks a lot for your help.


Comments

  • : Hi. I'm using Ms Access 2000. I have a table in which the data type of the primary key is a 'number'. Please tell me how can I automatically increment the value so that when I add a new record it automatically gives the record a unique value.
    :
    : Thanks a lot for your help.

    : hi i'm hassan
    you can do that by using autoincrese type of data to that field

    or if you are usinge a LONG data type to that field and you have a some data in the table you can flow this step:
    - add a new field to the table (data typ:AutoIcrese, incresing value not a random value)
    - erase the old field
    - change the name of the new field to the old field name
    - end
    if you have a data in the table you need a more proccessing and you can send me and i replay.

    thank




  • Hello there.

    John if you are using the 'Number' data type then you could always increment the value using a Visual Basic module. Do the following

    1. Using the button wizard, place a 'Next Record' button on your form. Rename it to "AddRecord".

    2. Switch to design view and click on the button's properties and then choose the onclick event handler.

    3. You will be presented with a module. It would be a private subrotine. I normally change remove the "Private" bit.

    4. Lets assume your field name is "IDField", declare it. Copy the following code

    Note: Assume the name of your primary key field is PrimFld.

    Sub AddRecord_Click()
    Dim IDField As Long
    On Error GoTo Err_AddRecord_Click

    Do.GoToRecord , , acLast

    IDField = PrimFld
    If IDField = 0 Then
    PrimFld = 1
    Else
    DoCmd.GoToRecord , , acNext
    PrimFld = IDField + 1
    End If

    Exit_AddRecord_Click:
    Exit Sub

    Hope you can easily follow the code and find it useful.

    Hasan.


  • Dear Hasan. Thank you so much for that code. It works perfectly fine. Well you see I don't know anything about programming.
    Once again thanks a lot for your help.

    John.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories