• We just launched and are currently in beta. Join us as we build and grow the community.

Auto Generate Key

GHJKL

Meta Builder
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
156
Likes
27
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
This tutorial will teach you on how to create a key automatically as a primary key in your table. It is sometimes needed to create your own key to format it on your own needs. Say for example you’d like to format it to start as four (4) digits rather than starting from one (1).

The code herein is based on the code that I have already submitted on some of my program like hotel reservation system. But these time a simplified one. I created this tutorial because there are some visitors in this website who asked this before.

In order to create an auto generated key for your table, all you need is a table that will hold the last key of the record that is auto generated. Below is the example of the table that we need using Microsoft Access.

[inline:Table.jpg=Table Schema]

Example of data within the “Key Generator” table.

[inline:Auto Generate Key Data.jpg=Auto Generate Key Data Sample]

As you can see at the above example every table has its own key for the next value.

The above record is incremented by one (1) once called.

The Code

In the form load event type the following code as shown below:

  1. Dim

    CustomerNo As

    Integer

  2. CustomerNo =

    GetIndex(

    "Customers"

    )
  3. txtCustomerNo.

    Text

    =

    CustomerNo

This will call a function called GetIndex and return the next value of the NextNo field.

The following is the code to return and increment a value for NextNo field:

  1. Public

    Function

    GetIndex(

    ByVal

    srcTable As

    String

    )

    As

    Long
  2. Dim

    cnCustomers As

    OleDbConnection
  3. 'create connection
  4. cnCustomers =

    New

    OleDbConnection

  5. With

    cnCustomers
  6. If

    .

    State

    =

    ConnectionState.

    Open

    Then

    .

    Close

    (

    )

  7. .

    ConnectionString

    =

    cnString
  8. .

    Open

    (

    )
  9. End

    With

  10. Dim

    sqlQRY As

    String

    =

    "SELECT * FROM [KEY GENERATOR] WHERE TableName = '"

    &

    srcTable &

    "'"

  11. 'create commands
  12. Dim

    cmd As

    OleDbCommand =

    New

    OleDbCommand(

    sqlQRY, cnCustomers)

  13. Try
  14. 'create data reader
  15. Dim

    rdr As

    OleDbDataReader =

    cmd.

    ExecuteReader
  16. Dim

    intNextNo As

    Integer

  17. 'loop through result set
  18. While

    (

    rdr.

    Read

    )
  19. intNextNo =

    rdr(

    "NextNo"

    )
  20. End

    While

  21. intNextNo =

    intNextNo +

    1

  22. 'define update statement
  23. 'this will update the table "key generator" to a new value
  24. Dim

    sqlUpdate As

    String

    =

    "UPDATE [Key Generator] SET [Key Generator].NextNo = "

    &

    intNextNo &

    " WHERE TableName='"

    &

    srcTable &

    "'"

  25. Dim

    cmdUpdate As

    OleDbCommand =

    New

    OleDbCommand(

    sqlUpdate, cnCustomers)

  26. 'execute nonquery to update an index
  27. cmdUpdate.

    ExecuteNonQuery

    (

    )

  28. GetIndex =

    intNextNo
  29. Catch

    ex As

    OleDbException
  30. MsgBox

    (

    ex, MsgBoxStyle.

    Information

    )
  31. Finally
  32. ' Close connection
  33. cnCustomers.

    Close

    (

    )
  34. Console.

    WriteLine

    (

    "Connection closed."

    )
  35. End

    Try
  36. End

    Function

I hope this will help some of you who do not know yet how to generate a key automatically.

 

452,292

323,340

323,349

Top