473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inserting records into MS Access where there is an autonumber for a primary key

7
Hi,

I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net

When someone wants to register on the website, they fill out a form and the contents of the form is inserted into the MS Access database.

The Customer table in the database already has 30 records (with CustomerIDs 1 - 30) in it (from when the database was first created). The CustomerID field in the database is an autonumber datatype and it is set to increment. However, new customers' information needs to be entered when they fill out the form. I am trying to now insert the information from the form into the database using VB.Net but the code i am using is inserting records at CustomerID 0.

I found many solutions on the net but none of them worked. When i don't use the autonumber field in the VALUES part of the query i get an error saying

“Number of query values and destination fields are not the same.”

When i actually get information to be inserted into the database it starts at CustomerID 0 instead of 31. Then if i try to input another record it is trying to overwrite the records i already have in the database. i get this error:

“The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.”

This is the code I am working with right now:

Dim connectionstring As String
connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("GameDB.mdb")
Expand|Select|Wrap|Line Numbers
  1. ...
  2.         Using myConnection As New OleDbConnection(connectionstring)
  3.         myConnection.Open()
  4.  
  5.         Dim cmdText As String = "SELECT * FROM Customer"
  6.         Dim command As OleDbCommand
  7.         command = New OleDbCommand(cmdText, myConnection)
  8.  
  9.         Dim reader As OleDbDataReader
  10.         reader = command.ExecuteReader
  11.  
  12.         Dim autonumber As Integer
  13.         Dim newauto As Integer
  14.  
  15.  
  16.         If autonumber > Microsoft.VisualBasic.Val("CustomerID")  Then
  17.         autonumber = Int(autonumber) + 1
  18.         newauto = autonumber
  19.         End If
  20.  
  21. OR
  22.  
  23. If autonumber = Microsoft.VisualBasic.Val("CustomerID") Then
  24.                 autonumber = Int(autonumber) + 1
  25.                 newauto = autonumber
  26.             End If
  27.  
  28. cmdText = "INSERT INTO Customer VALUES ('" + autonumber.ToString + "','" + FirstName.Text + "','" + LastName.Text + "','" + Address.Text + "','" + Country.Text + "','" + PhoneNo.Text + "','" + Email.Text + "','" + UserName1.Text + "','" + Password.Text + "')"
  29.         command = New OleDbCommand(cmdText, myConnection)
  30.         command.ExecuteNonQuery()
  31.         Response.Redirect("RegistrationApproved.aspx")
  32.  
  33.  
  34.         End If
  35.         End Using
Can anyone help me please. I have tried many different codes for this to work....
Jul 27 '08 #1
6 11733
ADezii
8,834 Expert 8TB
  1. Is the Name of your AutoNumber Field actually called autonumber?
  2. It appears as though you are trying to coerce the AutoNumber Field to an Integer, an AutoNumber Field is a Long Integer.
  3. You cannot dynamically assign a value to an AutoNumber Field, it will, and must, increment on its own.
  4. The AutoNumber Field should not be in the INSERT INTO Clause.
Jul 28 '08 #2
ashes
7
i got the code to work by naming the columns in the INSERT INTO statement. Before, i had only named the VALUES fields and not the columns in the database. Also the autonumber field (CustomerID) had to be left out. So the query now would be:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Customer (FirstName, LastName, Address ect...)    
  2. VALUES('"FirstName.Text + "','" + LastName.Text + "','" + Address.Text ect..)
Jul 28 '08 #3
ADezii
8,834 Expert 8TB
i got the code to work by naming the columns in the INSERT INTO statement. Before, i had only named the VALUES fields and not the columns in the database. Also the autonumber field (CustomerID) had to be left out. So the query now would be:

INSERT INTO Customer (FirstName, LastName, Address ect...)
VALUES('"FirstName.Text + "','" + LastName.Text + "','" + Address.Text ect..)
Thanks for sharing the solution with us, glad you worked it out.
Jul 28 '08 #4
I have similar but bit different issue that is

I have a table INVOICE when user enter data in invoice I have to insert record into Transactions (Master ) and Transaction_details (detail) both the tables have ID type autonumber i have a VB code that insert into record into Transaction table then when I try to insert record into transaction_details record I don't have the trasnsation ID which links these 2 tables. I am using MSaccess 2003. can any one help me in this.

Thanks ,
PP
Aug 4 '08 #5
NeoPa
32,556 Expert Mod 16PB
PP,

You have asked your question in another member's thread. This is called thread hijacking and is not allowed on this site. There are various reasons for this, the principal of which is that it typically diverts the flow of the thread away from his needs and into yours. Another is that it will tend to mean that your new question is not seen as such, and may get overlooked by many of our experts (You appreciate that they're not looking for answered questions).
Please post your questions in their own threads in future (See How to Ask a Question).

ADMIN.
Aug 4 '08 #6
NeoPa
32,556 Expert Mod 16PB
As to the original question, this is a frequently asked question which indicates a misunderstanding as to what AutoNumbers are for.

They are not designed to provide a sequential list. They are not designed to provide record sorting.

They are SIMPLY there to ensure that each record has a value which is different from every other value - Unique.

There are other ways of doing what you need, but they generally involve using fields other than AutoNumber fields.
Aug 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Robin Tucker | last post by:
Can someone explain what happens when two users concurrently attempt to create a new record in a table with an autonumber primary key? For example, user 1 creates a new record and manipulates it...
5
by: jonhanks | last post by:
I'm trying to copy a table and its structure from DBase into Access. One of the fields is a primary key and contains numbers from 0 to 20000 and then from 30000 to 40000. How can I make this...
0
by: RML | last post by:
Hi all, in my VB.NET app I would like to make a field in an Access Table the Primary Key. I've tried using an OleDBDataAdapter and DataTable with no success... Note: There is only 1 field in...
15
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
1
by: gouse | last post by:
Hello Friends, In a Table I am inserting more than 50,000 Records one by one. It was taking a lot of time . Is it There any good approach/solution for inserting records more than 50,000 one by one...
2
by: VSS | last post by:
Hello, I need to know what is the maximum number of records Access can hold? I need approx. 6 million records, do you know if Access can hold this amount? Thank you everyone.
2
by: masajid686 | last post by:
hi frnds I written the code for inserting records in sqlserver 2005 using dataset and command builder but i dont know hot write sp for that one. can anybody tellme Thanks
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
2
by: J Dillard | last post by:
I need to tie multiple records to one autonumber. For example, I am entering vendor savings into access. I need access to autonumber my first record for each vendor. Each vendor has multiple ship...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.