locked
Get Number of Records present in the table RRS feed

  • Question

  • User-1656904109 posted

    As a beginner to data access in VB.NET,I am using the examplary Northwind.mdb file to perform the basic display,add,delete and update operations on the Employees Table.What I want to know is the number of records already present so that I can add a new record at its next index/location.The following is the code:

    Dim nr As NorthwindDataSet.EmployeesRow
            nr = Me.NorthwindDataSet.Employees.NewEmployeesRow
            Dim x As Integer = NorthwindDataSet.Employees.Rows.Count
            nr.EmployeeID = x + 1
            MsgBox(x)
            nr.FirstName = TextBox1.Text
            nr.LastName = TextBox2.Text
            nr.City = TextBox3.Text
            nr.Country = TextBox4.Text
            Me.NorthwindDataSet.Employees.Rows.Add(nr)
            Me.EmployeesTableAdapter.Update(Me.NorthwindDataSet.Employees)

    But,calling this within a Button_Click event does add the desired fields,but the EmployeeID is all scrambled i.e. the message box shows x to be 9(when adding the first record) but the EmployeeID updated/added in the database is somewhat like 32/33 or some other number.

    Note:I am using TableAdapter

    Monday, May 7, 2012 10:14 AM

Answers

  • User-1199946673 posted

    The employeeID field is a so called autonumber field. Access will assign a new value for you, you don't need to worry about it....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 7, 2012 11:09 AM
  • User-1561814533 posted
    Autonumber fields are usually used on the primary key, they give you a unique value for the column and automatically increment. Some databases allow you to specify the start value and what to increment by.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 10, 2012 5:40 AM

All replies

  • User-1199946673 posted

    The employeeID field is a so called autonumber field. Access will assign a new value for you, you don't need to worry about it....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 7, 2012 11:09 AM
  • User-1656904109 posted

    Actually,I used a query statement to count the number of records by using the cmd.ExecuteScalar() command. By the way,what is an "autonumber field". Is it like the Primary key value ?(I have seen the Primary key value to be assigned the next integer everytime a new record is created.

    Thursday, May 10, 2012 5:31 AM
  • User-1561814533 posted
    Autonumber fields are usually used on the primary key, they give you a unique value for the column and automatically increment. Some databases allow you to specify the start value and what to increment by.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 10, 2012 5:40 AM