By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Insert

P: n/a
Hi All,
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string

con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test name')"
cmd.ExecuteNonQuery()

cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()

cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?

Thanks again!

Note: I am using an MS Access database

Nov 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
There is a syntax error in your second INSERT statement. You are missing
the opening parenthesis before the ID.

Beyond that, you might want to run the statements directly in Microsoft Access,
as it will give you a more complete error message.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Hi All,
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record
because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string
con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test
name')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()
cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?

Thanks again!

Note: I am using an MS Access database

Nov 13 '06 #2

P: n/a

Tim Patrick wrote:
There is a syntax error in your second INSERT statement. You are missing
the opening parenthesis before the ID.

Beyond that, you might want to run the statements directly in Microsoft Access,
as it will give you a more complete error message.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Hi All,
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record
because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string
con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test
name')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()
cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?

Thanks again!

Note: I am using an MS Access database

Tim,
Thank you for your quick reply. I have fixed the syntax error but I am
still getting the following error:

You cannot add or change a record because a related record is required
in table 'Table1'

Does MS Access support this type of transaction? Can you think of any
reason why the transaction wouldn't be working?

Thanks again!

Nov 13 '06 #3

P: n/a
Have you established any referrential integrity between the two tables, or
between any one of those two tables and other tables in your database? Have
you set up any field-specific rules? Do you have other fields in Table1 and
Table2 besides ID and Name that have referential integrity enabled, perhaps
to themselves or to each other? Did you get a chance to run the same statements
within the Access environment and check the error messages there?

I didn't see the Commit of the transaction in your code, but I assume you
are doing it just after the code block you pasted. Does the code work if
you move the commit to just after the first insert? What value do you get
back from the selection of MAX(ID)? I would think that the uncommitted transaction
would still return the right identity value for your statement within your
connection, but perhaps not.
-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Tim Patrick wrote:
>There is a syntax error in your second INSERT statement. You are
missing the opening parenthesis before the ID.

Beyond that, you might want to run the statements directly in
Microsoft Access, as it will give you a more complete error message.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005
>>Hi All,
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record
because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am
using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string
con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test
name')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()
cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?
Thanks again!

Note: I am using an MS Access database
Tim,
Thank you for your quick reply. I have fixed the syntax error but I am
still getting the following error:
You cannot add or change a record because a related record is required
in table 'Table1'

Does MS Access support this type of transaction? Can you think of any
reason why the transaction wouldn't be working?

Thanks again!

Nov 13 '06 #4

P: n/a

Tim Patrick wrote:
Have you established any referrential integrity between the two tables, or
between any one of those two tables and other tables in your database? Have
you set up any field-specific rules? Do you have other fields in Table1 and
Table2 besides ID and Name that have referential integrity enabled, perhaps
to themselves or to each other? Did you get a chance to run the same statements
within the Access environment and check the error messages there?

I didn't see the Commit of the transaction in your code, but I assume you
are doing it just after the code block you pasted. Does the code work if
you move the commit to just after the first insert? What value do you get
back from the selection of MAX(ID)? I would think that the uncommitted transaction
would still return the right identity value for your statement within your
connection, but perhaps not.
-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Tim Patrick wrote:
There is a syntax error in your second INSERT statement. You are
missing the opening parenthesis before the ID.

Beyond that, you might want to run the statements directly in
Microsoft Access, as it will give you a more complete error message.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Hi All,
Thank you in advance. I am trying to insert into two tables but I am
getting the following error: "You cannot add or change a record
because
a related record is required in table..." I am not sure why this is
happening since I am using transactions. Below is the code I am
using.
Dim con as OleDbConnection
Dim cmd as OleDbCommand
Dim tran as OleDbTranscation
Dim id as string
con = new OleDbConnection(connectionstring)
con.open
tran = con.BeginTransaction
cmd = new OleDbCommand
cmd.Connection = con
cmd.Transaction = tran
cmd.CommandText = "insert into table1 (id,name) values (1,'test
name')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select Max(id) from table1"
id = cmd.ExecuteScalar()
cmd.CommandText = "insert into table2 (id,name) values " & id &
",'another test name')"
cmd.ExecuteNonQuery()
Do you have any suggestions?
Thanks again!

Note: I am using an MS Access database
Tim,
Thank you for your quick reply. I have fixed the syntax error but I am
still getting the following error:
You cannot add or change a record because a related record is required
in table 'Table1'

Does MS Access support this type of transaction? Can you think of any
reason why the transaction wouldn't be working?

Thanks again!


Tim,
Thanks again for you help. I greatly appreciate it. It appears there is
a logic issue on my end. I removed all of the referrential integrity
and I am getting some very interesting results.

Thanks again for your assistance (and mentioning referrential
integrity) : )

Nov 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.