Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: How to do multiple insert statements


Message #1 by "Dai" <david@d...> on Sun, 9 Mar 2003 15:59:17
When doing a mulitiple insert into more than one table Do you need to join 
the insert statements with an ampersand or full stop etc.

I'm trying to insert info into five tables, but the only table that works 
is the review table.
Sql = "INSERT INTO review (review) VALUES (' " &comment& " ')"

browser out put when tested by Response.Write "Sql string = " & Sql
Sql string = INSERT INTO review (review) VALUES (' good basic hotel ')

I've checked all the form fields and variables and column names all are ok 
no typo's or errors.
This leads me to believe that, the way i've written the insert statements 
or there is wrong or something is fundamentally wrong with my database.

The code for the insert page:

<%
fname = Request ("name")
lname = Request ("lastname")
mail = Request ("email")
country = Request ("country")
resorts = Request ("resort")
accomtype = Request ("accomodation_type")
accomodations = Request ("accomodation")
comment = Request ("review")
 
Sql = "INSERT INTO users (email, firstname, lastname) VALUES (' " 
&mail& " ', ' " &fname& " ', ' "&lname& " ')"  
Sql = "INSERT INTO country (country_name) VALUES (' " &country& " ')"
Sql = "INSERT INTO resort (resort_name) VALUES (' " &resorts& " ')"
Sql = "INSERT INTO accomodation (accom_type, accom_name) VALUES (' " 
&accomtype& " ', ' " &accomodations& " ')"
Sql = "INSERT INTO review (review) VALUES (' " &comment& " ')"
Response.Write "Sql string = " & Sql
Set dbConn = Server.CreateObject("ADODB.Connection")
dbConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &  _
Server.MapPath("data\reviews.mdb") 
dbConn.Execute Sql
'Set objRS = Server.CreateObject("ADODB.Recordset")
'objRS.Open Sql, dbConn, 3

'Close databse Connection
dbConn.Close
Set dbConn = Nothing
%>

Message #2 by "Rob Parkhouse" <rparkhouse@o...> on Sun, 9 Mar 2003 22:51:44
> When doing a mulitiple insert into more than one table Do you need to 
join 
t> he insert statements with an ampersand or full stop etc.

> I'm trying to insert info into five tables, but the only table that 
works 
i> s the review table.
S> ql = "INSERT INTO review (review) VALUES (' " &comment& " ')"

> browser out put when tested by Response.Write "Sql string = " & Sql
S> ql string = INSERT INTO review (review) VALUES (' good basic hotel ')

> I've checked all the form fields and variables and column names all are 
ok 
n> o typo's or errors.
T> his leads me to believe that, the way i've written the insert 
statements 
o> r there is wrong or something is fundamentally wrong with my database.

> The code for the insert page:

> <%
f> name = Request ("name")
l> name = Request ("lastname")
m> ail = Request ("email")
c> ountry = Request ("country")
r> esorts = Request ("resort")
a> ccomtype = Request ("accomodation_type")
a> ccomodations = Request ("accomodation")
c> omment = Request ("review")
 > 
S> ql = "INSERT INTO users (email, firstname, lastname) VALUES (' " 
&> mail& " ', ' " &fname& " ', ' "&lname& " ')"  
S> ql = "INSERT INTO country (country_name) VALUES (' " &country& " ')"
S> ql = "INSERT INTO resort (resort_name) VALUES (' " &resorts& " ')"
S> ql = "INSERT INTO accomodation (accom_type, accom_name) VALUES (' " 
&> accomtype& " ', ' " &accomodations& " ')"
S> ql = "INSERT INTO review (review) VALUES (' " &comment& " ')"
R> esponse.Write "Sql string = " & Sql
S> et dbConn = Server.CreateObject("ADODB.Connection")
d> bConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &  _
S> erver.MapPath("data\reviews.mdb") 
d> bConn.Execute Sql
'> Set objRS = Server.CreateObject("ADODB.Recordset")
'> objRS.Open Sql, dbConn, 3

> 'Close databse Connection
d> bConn.Close
S> et dbConn = Nothing
%> >



You keep overwriting the content of variable "sql" so of course the only 
one that works is the last one. Put the dbconn.Execute sql statement after 
each sql = "INSERT..." statement and you might have more success.

Good Luck

  Return to Index