473,776 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add a record for access with an AutoNumber field

HS1
Hello

I have a table in Access Database. This table has a AutoNumber field. I use
a DataGrid to show that table

When I insert a new record in for this table using a DataGrid, there is a
message that asks me to enter the value for this AutoNumber field. Why I
have to do that?

Could you please help
Thank you
S.Hoa
Nov 21 '05 #1
6 6234
Check the XSD file containing the table the AutoField should have the
attribute AutoIncrement=" true"
EX.
<xs:element name="LocationI d" msdata:AutoIncr ement="true" type="xs:int" />

Brad Shook

"HS1" <so*@slingshot. co.nz> wrote in message
news:1098149437 .839697@ftpsrv1 ...
Hello

I have a table in Access Database. This table has a AutoNumber field. I use a DataGrid to show that table

When I insert a new record in for this table using a DataGrid, there is a
message that asks me to enter the value for this AutoNumber field. Why I
have to do that?

Could you please help
Thank you
S.Hoa

Nov 21 '05 #2
HS1
Thank you
But here is WindowsForms

"Brad Shook" <bs****@echd.or g.removeme> wrote in message
news:Ou******** ******@TK2MSFTN GP10.phx.gbl...
Check the XSD file containing the table the AutoField should have the
attribute AutoIncrement=" true"
EX.
<xs:element name="LocationI d" msdata:AutoIncr ement="true" type="xs:int" />

Brad Shook

"HS1" <so*@slingshot. co.nz> wrote in message
news:1098149437 .839697@ftpsrv1 ...
Hello

I have a table in Access Database. This table has a AutoNumber field. I

use
a DataGrid to show that table

When I insert a new record in for this table using a DataGrid, there is a message that asks me to enter the value for this AutoNumber field. Why I
have to do that?

Could you please help
Thank you
S.Hoa


Nov 21 '05 #3
S. Hoa,

Brad shows you the XSD generated when you use the designer and a strongly
typed dataset.

This kind of questions are almost impossible to answer withouth some code.
You see it yourself. Nobody know how you do it, there are so many
possibilities, so the least what you would show in my opinion is show some
code (10 rows) how you add that row.

Or is it done alone with the * than tell that.

Your problem is probably around the autoincrement and the three properties
from that.

http://msdn.microsoft.com/library/de...ementtopic.asp

I hope this helps?

Cor

"HS1" <so*@slingshot. co.nz>
But here is WindowsForms

"Brad Shook" <bs****@echd.or g.removeme> wrote in message
news:Ou******** ******@TK2MSFTN GP10.phx.gbl...
Check the XSD file containing the table the AutoField should have the
attribute AutoIncrement=" true"
EX.
<xs:element name="LocationI d" msdata:AutoIncr ement="true" type="xs:int"
/>

Brad Shook

"HS1" <so*@slingshot. co.nz> wrote in message
news:1098149437 .839697@ftpsrv1 ...
> Hello
>
> I have a table in Access Database. This table has a AutoNumber field. I

use
> a DataGrid to show that table
>
> When I insert a new record in for this table using a DataGrid, there is a > message that asks me to enter the value for this AutoNumber field. Why
> I
> have to do that?
>
> Could you please help
> Thank you
> S.Hoa
>
>



Nov 21 '05 #4
HS1
Thank you Cor
I will explain more details

I have a table of Access Database. There is an field ID with AutoNumber type
that is the primary key in this table.

I show the data of this table in a DataGrid using a dataset with a
connection.

I have some TextBox(es) that present data of the current record in the
DataGrid by using data Binding (of course, there is a textbox to present
ID).

You can see that it is ver common. Everthing works fine.

Now I want to add a new record into this table (or datagrid). When I click
add new button with the code

Me.BindingConte xt(DataGrid1.Da taSource, "Clients").AddN ew()

all TextBox(es) are empty then I can enter new data for the new record. I do
not enter value for the ID textbox field as it is AutoNumber. However, when
I click Update,

da1.Update(ds)

there is message that ask me the fiel ID shoud not be Null. Why I have to do
that when I already set that field as AutoNumber type (of course, I set when
I create that table in Access).

Is it clear???
Thanks
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:u7******** ******@TK2MSFTN GP15.phx.gbl...
S. Hoa,

Brad shows you the XSD generated when you use the designer and a strongly
typed dataset.

This kind of questions are almost impossible to answer withouth some code.
You see it yourself. Nobody know how you do it, there are so many
possibilities, so the least what you would show in my opinion is show some
code (10 rows) how you add that row.

Or is it done alone with the * than tell that.

Your problem is probably around the autoincrement and the three properties
from that.

http://msdn.microsoft.com/library/de...ementtopic.asp
I hope this helps?

Cor

"HS1" <so*@slingshot. co.nz>
But here is WindowsForms

"Brad Shook" <bs****@echd.or g.removeme> wrote in message
news:Ou******** ******@TK2MSFTN GP10.phx.gbl...
Check the XSD file containing the table the AutoField should have the
attribute AutoIncrement=" true"
EX.
<xs:element name="LocationI d" msdata:AutoIncr ement="true" type="xs:int"
/>

Brad Shook

"HS1" <so*@slingshot. co.nz> wrote in message
news:1098149437 .839697@ftpsrv1 ...
> Hello
>
> I have a table in Access Database. This table has a AutoNumber field. I use
> a DataGrid to show that table
>
> When I insert a new record in for this table using a DataGrid, there is
a
> message that asks me to enter the value for this AutoNumber field.

Why > I
> have to do that?
>
> Could you please help
> Thank you
> S.Hoa
>
>



Nov 21 '05 #5
S Hoa

I made a complete sample for you what should fit your problem completly

The first part is to make a minimum accessdatabase as you told, therefore
do not become affraid of that part. You can paste this in a form and need a
datagrid a textbox and a button on the form. As well do you have to set a
reference to reference to COM adox ext 2.x for dll and security (that is for
creating the testdatabase). The directory is in this case C:\test1. I hope
this helps, and expect an answer from you, that I am sure you saw this,
otherwise you know probably what I want to say.

I hope this helps?

Cor

\\\
Dim dv As DataView
Private Sub Form1_Load(ByVa l sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Dim catNewDB As New ADOX.Catalog
Dim fi As New IO.FileInfo("c: \test1\db1.mdb" )
If fi.Exists Then
If MessageBox.Show ("Delete?", "Existing File db1.mdb", _
MessageBoxButto ns.YesNo) = DialogResult.Ye s Then
fi.Delete()
Else
Exit Sub
End If
End If
catNewDB.Create ("Provider=Micr osoft.Jet.OLEDB .4.0;" & _
"Data Source=C:\test1 \db1.mdb")
'To make tables we use Adonet
Dim conn As New
OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;" & _
" Data Source=C:\test1 \db1.mdb;User Id=admin;Passwo rd=;")
Dim cmd As New OleDb.OleDbComm and("CREATE TABLE persons ( " & _
"AutoId int identity ," & _
"Name NVarchar(50)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
conn.Open()
Try
cmd.ExecuteNonQ uery()
Catch ex As OleDb.OleDbExce ption
MessageBox.Show (ex.Message, "OleDbException ")
Exit Sub
Catch ex As Exception
MessageBox.Show (ex.Message, "GeneralExcepti on")
Exit Sub
End Try
conn.Close()
Dim da As New OleDb.OleDbData Adapter("Select * from Persons", conn)
Dim ds As New DataSet
da.Fill(ds)
ds.Tables(0).Co lumns("AutoId") .AutoIncrement = True
ds.Tables(0).Co lumns("AutoId") .AutoIncrementS eed = -1
ds.Tables(0).Co lumns("AutoId") .AutoIncrementS tep = -1
dv = New DataView(ds.Tab les(0))
ds.Tables(0).Ro ws.Add(ds.Table s(0).NewRow)
ds.Tables(0).Ro ws(0)("Name") = "Cor"
dv.AllowNew = False
DataGrid1.DataS ource = dv
TextBox1.DataBi ndings.Add("Tex t", dv, "Name")
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
dv.AllowNew = True
dv.AddNew()
dv.AllowNew = False
End Sub
///
Nov 21 '05 #6
HS1
Thank you very much for your help, Cor

I understand clearly your code. You create a table for a database db1.mdb
and then set AutoNumber for the Identity field

My table was ALREADY created in a database using Access Database. When I
created this table, I ALREADY set the field ID is AutoNumber. That means
the table with a field AutoNumber was created before I build a VB.Net
application. Now I have to create this VB.Net for entering new data (of
course, there is old data in this table).

I try a part of your code when the form is loaded:

ds.Tables(0).Co lumns("AutoId") .AutoIncrement = True

It work OK now

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:OG******** ******@tk2msftn gp13.phx.gbl...
S Hoa

I made a complete sample for you what should fit your problem completly

The first part is to make a minimum accessdatabase as you told, therefore
do not become affraid of that part. You can paste this in a form and need a datagrid a textbox and a button on the form. As well do you have to set a
reference to reference to COM adox ext 2.x for dll and security (that is for creating the testdatabase). The directory is in this case C:\test1. I hope
this helps, and expect an answer from you, that I am sure you saw this,
otherwise you know probably what I want to say.

I hope this helps?

Cor

\\\
Dim dv As DataView
Private Sub Form1_Load(ByVa l sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Dim catNewDB As New ADOX.Catalog
Dim fi As New IO.FileInfo("c: \test1\db1.mdb" )
If fi.Exists Then
If MessageBox.Show ("Delete?", "Existing File db1.mdb", _
MessageBoxButto ns.YesNo) = DialogResult.Ye s Then
fi.Delete()
Else
Exit Sub
End If
End If
catNewDB.Create ("Provider=Micr osoft.Jet.OLEDB .4.0;" & _
"Data Source=C:\test1 \db1.mdb")
'To make tables we use Adonet
Dim conn As New
OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;" & _
" Data Source=C:\test1 \db1.mdb;User Id=admin;Passwo rd=;")
Dim cmd As New OleDb.OleDbComm and("CREATE TABLE persons ( " & _
"AutoId int identity ," & _
"Name NVarchar(50)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
conn.Open()
Try
cmd.ExecuteNonQ uery()
Catch ex As OleDb.OleDbExce ption
MessageBox.Show (ex.Message, "OleDbException ")
Exit Sub
Catch ex As Exception
MessageBox.Show (ex.Message, "GeneralExcepti on")
Exit Sub
End Try
conn.Close()
Dim da As New OleDb.OleDbData Adapter("Select * from Persons", conn) Dim ds As New DataSet
da.Fill(ds)
ds.Tables(0).Co lumns("AutoId") .AutoIncrement = True
ds.Tables(0).Co lumns("AutoId") .AutoIncrementS eed = -1
ds.Tables(0).Co lumns("AutoId") .AutoIncrementS tep = -1
dv = New DataView(ds.Tab les(0))
ds.Tables(0).Ro ws.Add(ds.Table s(0).NewRow)
ds.Tables(0).Ro ws(0)("Name") = "Cor"
dv.AllowNew = False
DataGrid1.DataS ource = dv
TextBox1.DataBi ndings.Add("Tex t", dv, "Name")
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
dv.AllowNew = True
dv.AddNew()
dv.AllowNew = False
End Sub
///

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2728
by: Frances | last post by:
Hi All, I'm having a problem trying to add a record to a simple Access 2000 db (db is very similar to an address book but with more info than the usual address, phone, etc.). The database is one table, 36 fields. The record_id field is an autonumber field (long int) and primary key. The rest of the fields comprise of 30 text fields, 3 memo fields, 1 date field and 1 currency field. Text fields vary in number of characters allowed. ...
5
4335
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one child table (StudentProgress). The course progress records how a student progresses on a course. I have one course (History) and one student called Maya. I now want to record her grade (64). If I do this in Access using a form, then the form...
5
3753
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for Office 2000. I am creating a database to track student athletes. I have created the following tables. The table title is to the far left, with fields under each. The common field will be the StudentID field, which is their student number assigned...
9
9070
by: DS | last post by:
Whenever you use a continous form each row represents a record. Does this record have a number? If so how do you access it. Right now I have a field using auto number. But if access generates its own number for that row I'd like to use that. Thanks DS
0
1807
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the database so I could sort the records by the date at which they were entered. Well now I've deleted some of those records so its of course causing gaps in the records. The record number in Access no longer matches my record number that I...
1
5420
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the database so I could sort the records by the date at which they were entered. Well now I've deleted some of those records so its of course causing gaps in the records. The record number in Access no longer matches my record number that I...
6
2746
by: AA Arens | last post by:
Hi, I have a database with 2 main forms. Contacts and companies. I share the base with two others via LAN. On the companies form I have buttons to navigate throught the records (>400). We are mostly handling let say 5 companies. Every time I have to navigate or choose the find record button to get the right company. I am looking fo a feature to have listed in a combo list the last 5 visited records ("recently visited records").
10
12717
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be certain that MyVar will be set to the key-field value that was created when the Append query ran. Now, there are other ways to do it - I know - that will ensure you 'nab' the correct record. But I was wondering
5
8830
by: payffl | last post by:
My users are running Access '03. They have a database with a form that allows them to enter new records. Frequently they will move to a new record and not enter any information. This prevents the record from writing to the table, and worse, it prevents anyone else from opening the database (db has been placed in a state by user....etc.) I haven't been able to figure a way to force the autonumber to generate with an event on the new...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5368
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.