Creating new records in MSACCESS Table

Derek Napoli

I am attempting to create a new record from vb.net to an msaccess table, which i am able to do, but i have to add in the next consecutive ID number for it to actually save. For instance, if the next ID in the Access DB is 4, i have to type in 4 in the id textfield on my form. Code below:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim sqlinsert As String
    ' We use the INSERT statement which tells our program to add the information  
    ' from the Forms Text fields into the Databases columns.   
    sqlinsert = "INSERT INTO Table1(Title, YearofFilm, Description, Field1, ID)" & _
    "VALUES(@Title, @YearofFilm, @Description, @Field1, @ID)"
    Dim cmd As New OleDbCommand(sqlinsert, con1)
    ' This assigns the values for our columns in the DataBase.   
    ' To ensure the correct values are written to the correct column  
    cmd.Parameters.Add(New OleDbParameter("@Title", TextBox1.Text))
    cmd.Parameters.Add(New OleDbParameter("@YearofFilm", Convert.ToInt32(TextBox2.Text)))
    cmd.Parameters.Add(New OleDbParameter("@Description", TextBox3.Text))
    cmd.Parameters.Add(New OleDbParameter("@Field1", TextBox4.Text))
    cmd.Parameters.Add(New OleDbParameter("@ID", Convert.ToInt32(TextBox5.Text)))
    ' This is what actually writes our changes to the DataBase.  
    ' You have to open the connection, execute the commands and  
    ' then close connection.  
    con1.Open()
    cmd.ExecuteNonQuery()
    con1.Close()
    ' This are subs in Module1, to clear all the TextBoxes on the form  
    ' and refresh the DataGridView on the MainForm to show our new records.  
    ClearTextBox(Me)
    RefreshDGV()
    Me.Close()
End Sub

How can i tell textbox5 which is the ID field, to be the next number in the access db?

Steve

Open your Access database, show the structure of your table and change the ID field type from numeric to AutoNumber.

Now your code don't need to pass anything to Access because the number will be handled automatically from Access.

You could just add these lines to your code to get back the number assigned by Access to your field

Dim sqlinsert As String
sqlinsert = "INSERT INTO Table1(Title, YearofFilm, Description, Field1)" & _
            "VALUES(@Title, @YearofFilm, @Description, @Field1)"
Dim cmd As New OleDbCommand(sqlinsert, con1)
cmd.Parameters.Add(New OleDbParameter("@Title", TextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@YearofFilm", Convert.ToInt32(TextBox2.Text)))
cmd.Parameters.Add(New OleDbParameter("@Description", TextBox3.Text))
cmd.Parameters.Add(New OleDbParameter("@Field1", TextBox4.Text))
con1.Open()
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
cmd.CommandText = "SELECT @@IDENTITY"
Dim assignedID = Convert.ToInt32(cmd.ExecuteScalar())
' Eventually
TextBox5.Text = assignedID.ToString
con1.Close()
......

See also How to retrieve last autoincremented value in MS-Access like @@Identity in Sql Server

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MSAccess: Get ItemIDs from Recordsetclone into New Table?

From Dev

ON DUPLICATE KEY creating new records

From Dev

Schedulable Class not creating new records - Not doing anything

From Dev

cakephp: creating new records from other controller

From Dev

Creating new related records with Ember Data

From Dev

creating new field for certain records in miniMongo

From Dev

Qt MySQL - Creating New Table

From Dev

Updating table on creating a new row in another table

From Dev

Creating a new record resulting in creating identic new record in related table

From Dev

creating unique new column id by selecting first 7 characters from a column X and inserting into the new column Y for 10 000 records within same table

From Dev

Store records in a new table created by a query in mysql

From Dev

drop table and insert records from a new select statement (with the correct records)

From Dev

Insert new Entity with one to many relationship not creating records in both tables

From Dev

Why is laravel's updateOrCreate creating new records instead of updating?

From Dev

Insert new Entity with one to many relationship not creating records in both tables

From Dev

Creating a new cell on a table border in LibreOffice

From Dev

Using VARCHAR(255) - Creating a new MySQL table

From Dev

PHP MySQL database not creating new table

From Dev

Creating new table in Laravel based on user input?

From Dev

Creating new columns in data.table

From Dev

Creating a new table in Visual Studio 2013

From Dev

creating new record in another table for each user

From Dev

PHP artisan migrate not creating new table

From Dev

Creating new personal table for user in SQLAlchemy

From Dev

New Grails domain class is not creating a table in the database

From Dev

Creating a new cell on a table border in LibreOffice

From Dev

Creating new table from columns in multiple tables

From Dev

PHP MySQL database not creating new table

From Dev

Creating new columns in for loop data.table

Related Related

  1. 1

    MSAccess: Get ItemIDs from Recordsetclone into New Table?

  2. 2

    ON DUPLICATE KEY creating new records

  3. 3

    Schedulable Class not creating new records - Not doing anything

  4. 4

    cakephp: creating new records from other controller

  5. 5

    Creating new related records with Ember Data

  6. 6

    creating new field for certain records in miniMongo

  7. 7

    Qt MySQL - Creating New Table

  8. 8

    Updating table on creating a new row in another table

  9. 9

    Creating a new record resulting in creating identic new record in related table

  10. 10

    creating unique new column id by selecting first 7 characters from a column X and inserting into the new column Y for 10 000 records within same table

  11. 11

    Store records in a new table created by a query in mysql

  12. 12

    drop table and insert records from a new select statement (with the correct records)

  13. 13

    Insert new Entity with one to many relationship not creating records in both tables

  14. 14

    Why is laravel's updateOrCreate creating new records instead of updating?

  15. 15

    Insert new Entity with one to many relationship not creating records in both tables

  16. 16

    Creating a new cell on a table border in LibreOffice

  17. 17

    Using VARCHAR(255) - Creating a new MySQL table

  18. 18

    PHP MySQL database not creating new table

  19. 19

    Creating new table in Laravel based on user input?

  20. 20

    Creating new columns in data.table

  21. 21

    Creating a new table in Visual Studio 2013

  22. 22

    creating new record in another table for each user

  23. 23

    PHP artisan migrate not creating new table

  24. 24

    Creating new personal table for user in SQLAlchemy

  25. 25

    New Grails domain class is not creating a table in the database

  26. 26

    Creating a new cell on a table border in LibreOffice

  27. 27

    Creating new table from columns in multiple tables

  28. 28

    PHP MySQL database not creating new table

  29. 29

    Creating new columns in for loop data.table

HotTag

Archive