473,399 Members | 3,603 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,399 software developers and data experts.

insert into syntax

I have problems writing data from my webform to two linked tables. I am quiet sure that my insert into syntax is not correct. Please take a look if you have a moment, if not I understand:
Expand|Select|Wrap|Line Numbers
  1. <%
  2.    ' Declaring variables
  3.    Dim name, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert, sql_insertOne
  4.  
  5.    ' A Function to check if some field entered by user is empty
  6.    Function ChkString(string)
  7.    If string = "" Then string = " "
  8.    ChkString = Replace(string, "'", "''")
  9.    End Function
  10.  
  11.    ' Receiving values from Form
  12.    name = ChkString(Request.Form("name"))
  13.    age = ChkString(Request.Form("age"))
  14.    dateDB = ChkString(Request.Form("dateDB"))
  15.    QuestionOne = ChkString(Request.Form("QuestionOne"))
  16.  
  17.  
  18.    data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _ 
  19.             Server.MapPath("form.mdb")
  20.  
  21.    sql_insert = "insert into users (name, age, dateDB) values ('" & _
  22.             name & "', '" & age & "', '" & dateDB & "'); insert into Diagnosis (QuestionOne) values ('" & _ QuestionOne & "')"
  23.  
  24.  
  25.    ' Creating Connection Object and opening the database
  26.    Set con = Server.CreateObject("ADODB.Connection")
  27.  
  28.    con.Open data_source
  29.    con.Execute sql_insert
  30.    ' Done. Close the connection
  31.    con.Close
  32.    Set con = Nothing
  33.  
  34.  
  35.    Response.Write "All records were successfully entered into the database."
  36.  %>
Feb 20 '08 #1
18 2083
DrBunchman
979 Expert 512MB
Hi there,

Are you recieving an error when you run your code?

Dr B
Feb 20 '08 #2
CroCrew
564 Expert 512MB
Hello dbertanjoli,

Give this a try. And like DrBunchman had asked please post any errors that you get when replying or posting new questions along with your code. It helps us in helping you.

Hope it helps~

Expand|Select|Wrap|Line Numbers
  1. <%
  2.     ' Declaring variables
  3.     Dim name, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert, sql_insertOne
  4.  
  5.     ' A Function to check if some field entered by user is empty
  6.     Function ChkString(string)
  7.         If string = "" Then 
  8.             string = " "
  9.         End If
  10.         ChkString = Replace(string, "'", "''")
  11.     End Function
  12.  
  13.     ' Receiving values from Form
  14.     name = ChkString(Request.Form("name"))
  15.     age = ChkString(Request.Form("age"))
  16.     dateDB = ChkString(Request.Form("dateDB"))
  17.     QuestionOne = ChkString(Request.Form("QuestionOne"))
  18.  
  19.     Set Conn = Server.CreateObject("ADODB.Connection")
  20.         Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb") 
  21.  
  22.             SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "')"
  23.         Conn.Execute(SQL)
  24.             SQL = "insert into Diagnosis (QuestionOne) values ('" & _ QuestionOne & "')"
  25.         Conn.Execute(SQL)
  26.  
  27.     Conn.Close
  28.     Set Conn = Nothing
  29.  
  30.     Response.Write "All records were successfully entered into the database."
  31. %>
  32.  
Feb 20 '08 #3
Hello,
Thank you very much for your reply. I pasted the code above and this is a message I am getting: (two tables are linked by idName)

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC Microsoft Access Driver] You cannot add or change a record because a related record is required in table 'users'.

/2000040101/form_ac.asp, line 34

I would greatly appreciate your reply.

Debbie
Feb 20 '08 #4
CroCrew
564 Expert 512MB
It sounds like your tables have a relationship. Can you find out what needs to be passed in to the “Diagnosis” table from the “Users” table. Then we can edit the code to insert that information.
Feb 20 '08 #5
Hi CroCrew,

table users:
idName
name
age
dateDB

table:
Diagnosis:
idName
QuestionOne

They are linke by idName.

Thank you very much for your help.

Debbie
Feb 20 '08 #6
jhardman
3,406 Expert 2GB
Hi CroCrew,

table users:
idName
name
age
dateDB

table:
Diagnosis:
idName
QuestionOne

They are linke by idName.

Thank you very much for your help.

Debbie
I noticed that the idName field is not added by your sql insert statement. Is this because it is an auto-generated field? If so, I have scratched my head over this problem a couple times already. The only solution I have found is to add the first record, then search for it to pull up the auto-generated field, then use it when inserting the second record. However, I was recently brushing up my SQL and I saw this possibility:
Expand|Select|Wrap|Line Numbers
  1. SQL = "insert into users (name, age, dateDB) values ('" & name & "', '"
  2. sql = sql & age & "', '" & dateDB & "');"
  3. sql = sql & "insert into Diagnosis (QuestionOne, idName) values ('" 
  4. sql = sql & QuestionOne & "', (SELECT idName FROM users WHERE name = '"
  5. sql = sql & name & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
I haven't tried it, but it should work. Let me know. Anyone else see a solution?

Jared
Feb 21 '08 #7
Hello Jared,
This is updated code and I am still receiving same error message (idName is autonumber):

Expand|Select|Wrap|Line Numbers
  1. <%
  2.     ' Declaring variables
  3.     Dim name, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert, sql_insertOne
  4.  
  5.     ' A Function to check if some field entered by user is empty
  6.     Function ChkString(string)
  7.         If string = "" Then 
  8.             string = " "
  9.         End If
  10.         ChkString = Replace(string, "'", "''")
  11.     End Function
  12.  
  13.     ' Receiving values from Form
  14.     name = ChkString(Request.Form("name"))
  15.     age = ChkString(Request.Form("age"))
  16.     dateDB = ChkString(Request.Form("dateDB"))
  17.     QuestionOne = ChkString(Request.Form("QuestionOne"))
  18.  
  19.     Set Conn = Server.CreateObject("ADODB.Connection")
  20.         Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb") 
  21.  
  22.  SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "');"
  23. sql = sql & "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "', (SELECT idName FROM users WHERE name = '" & name & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
  24.  
  25.  Conn.Execute(SQL)
  26.  
  27.  
  28.  Conn.Close
  29.  Set Conn = Nothing
  30.  
  31.     Response.Write "All records were successfully entered into the database."
  32. %>
Feb 21 '08 #8
jhardman
3,406 Expert 2GB
I'm not ready to give up this approach yet! Lets split up the two commands as CroCrew suggested, then confirm that the error is coming from the SECOND execute line and not the first:
Expand|Select|Wrap|Line Numbers
  1.  SQL = "insert into users (name, age, dateDB) values ('" & name 
  2. sql = sql & "', '" & age & "', '" & dateDB & "')"
  3.  Conn.Execute(SQL)
  4.  
  5. sql = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne
  6. sql = sql & "', (SELECT idName FROM users WHERE name = '" & name 
  7. sql = sql & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
  8.  Conn.Execute(SQL)
Is the error coming from the second execute line?

Jared
Feb 22 '08 #9
Hi Jared,
I already tried this... same message I received :-)

Error is coming from line 34:
Expand|Select|Wrap|Line Numbers
  1. SQL =  "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "', (SELECT idName FROM users WHERE name = '" & name & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
Deb
Feb 22 '08 #10
markrawlingson
346 Expert 100+
I'm not sure why you're recieving this error message, congratulations you've got me stumped. :P

But it's definitely because of a relationship between the two tables, so why don't you open a recordset to the two tables with your sql statement containing a select statement with an SQL join. Then update the information using rs("column_name") statements? There shouldn't be a relationship issue at that point if you use a proper join type.

If you still get the same issue - send me the database (it's ms access right???) and I'll run some SQL passed it until I find a solution for you :P

Sincerely,
Mark


Hi Jared,
I already tried this... same message I received :-)

Error is coming from line 34:
SQL = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "', (SELECT idName FROM users WHERE name = '" & name & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"

Deb
Feb 22 '08 #11
Hello Experts
I followed Mark's advise and I got this message: I am attaching my access database (I stripped all the unnecessary parts from it).
__________________________________________________ _
Microsoft VBScript compilation error '800a03fd'

Expected 'Case'

/2000040101/form_ac.asp, line 33
Expand|Select|Wrap|Line Numbers
  1. SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName
  2. -------^
  3. __________________________________________________________________
  4. <%
  5.     ' Declaring variables
  6.     Dim name, age, dateDB, QuestionOne, data_source, con, sql_insert, rs
  7.  
  8.     ' A Function to check if some field entered by user is empty
  9.     Function ChkString(string)
  10.         If string = "" Then 
  11.             string = " "
  12.         End If
  13.         ChkString = Replace(string, "'", "''")
  14.     End Function
  15.  
  16.     ' Receiving values from Form
  17.     name = ChkString(Request.Form("name"))
  18.     age = ChkString(Request.Form("age"))
  19.     dateDB = ChkString(Request.Form("dateDB"))
  20.     QuestionOne = ChkString(Request.Form("QuestionOne"))
  21.  
  22.     Set Conn = Server.CreateObject("ADODB.Connection")
  23.         Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb") 
  24.  
  25.  
  26. SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName 
  27.  
  28.  
  29.  
  30. rs.Open SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "')"  
  31.  Conn.Execute(SQL)
  32.  
  33. rs.Open SQL =  "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "')"  
  34.  
  35. Conn.Execute(SQL)
  36.  
  37.  
  38.  Conn.Close
  39.  Set Conn = Nothing
  40.  
  41.     Response.Write "All records were successfully entered into the database."
  42. %>
Many thanks for any help.
Debbie
Attached Files
File Type: zip form.zip (12.6 KB, 131 views)
Feb 22 '08 #12
NeoPa
32,556 Expert Mod 16PB
Silly question, but would [Age] & [Date] both be string variables (as per your SQL)?

PS. Debbie (and any others it pertains to), please remember to use the [ CODE ] tags (# button) when including code (As per comment on original post).

Good luck with your problem and Welcome to TheScripts.
Feb 22 '08 #13
Hello,

Age is a string, dateDB is Date/Time in my database.

I just changed my script, AGAIN, (changed field names) to avoid this message I am gettting (but didn't help):
------------------------------------------------
Microsoft OLE DB Provider for ODBC Drivers error '80004005'

[Microsoft][ODBC Microsoft Access Driver] Reserved error (|); there is no message for this error.

/2000040101/form_ac.asp, line 37
------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     ' Declaring variables
  3.     Dim FullName, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert
  4.  
  5.     ' A Function to check if some field entered by user is empty
  6.     Function ChkString(string)
  7.         If string = "" Then 
  8.             string = " "
  9.         End If
  10.         ChkString = Replace(string, "'", "''")
  11.     End Function
  12.  
  13.     ' Receiving values from Form
  14.     name = ChkString(Request.Form("FullName"))
  15.     age = ChkString(Request.Form("age"))
  16.     dateDB = ChkString(Request.Form("dateDB"))
  17.     QuestionOne = ChkString(Request.Form("QuestionOne"))
  18.  
  19.     Set Conn = Server.CreateObject("ADODB.Connection")
  20.         Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb") 
  21.  
  22. sql_insert = "insert into users (FullName, age, dateDB) values ('" & FullName & "', '" & age & "', '" & dateDB & "')"
  23.  Conn.Execute(sql_insert)
  24.  
  25.  
  26. sql_insert = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "', (SELECT idName FROM users WHERE FullName = '" & FullName & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
  27.  
  28.  Conn.Execute(sql_insert)
  29.  
  30.  
  31.  Conn.Close
  32.  Set Conn = Nothing
  33.  
  34.     Response.Write "All records were successfully entered into the database."
  35. %>
Please help,
Debbie
Feb 22 '08 #14
NeoPa
32,556 Expert Mod 16PB
I swear you must have edited your post after I looked at it last time :S
If [dateDB] is a Date/Time field then your line 26 is wrong. The value needs to be formatted as a date and surrounded by "#" separators. Look in Literal DateTimes and Their Delimiters (#) for a full rundown on this point.
Good luck.
Feb 22 '08 #15
Hello,
Just for the sake of testing I changed it to string and still the same error message. I don'w know what else to try.
deb
Feb 22 '08 #16
NeoPa
32,556 Expert Mod 16PB
Do you mean you redesigned the table so that the field dateDB was stored as a string?
That wasn't my suggestion, but as long as you don't expect the query to work, it should test the compilation at least.
Feb 22 '08 #17
CroCrew
564 Expert 512MB
Hello dbertanjoli,


Sorry, back now. I look at your database and everything looks good. You do have a relationship between the two tables. Try this working example. It should work for you with the database that you linked above for us to look at. The example is named Example.asp

Hope this helps~

Example.asp
Expand|Select|Wrap|Line Numbers
  1. <%
  2.     Function ChkString(string)
  3.         If string = "" Then 
  4.             string = " "
  5.         End If
  6.         ChkString = Replace(string, "'", "''")
  7.     End Function
  8.  
  9.     If (Request.Form("xSubmit")) Then
  10.         LocalVarName = ChkString(Request.Form("xname"))
  11.         LocalVarAge = ChkString(Request.Form("xage"))
  12.         LocalVarDateDB = ChkString(Request.Form("xdateDB"))
  13.         LocalVarQuestionOne = ChkString(Request.Form("xQuestionOne"))
  14.  
  15.         Set Conn = Server.CreateObject("ADODB.Connection")
  16.         Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb") 
  17.  
  18.         ' Add record into Users table
  19.             Set rsUsers = CreateObject("ADODB.Recordset")
  20.             rsUsers.CursorLocation = 3
  21.             rsUsers.Open "SELECT TOP 1 * FROM users", Conn, 3, 3
  22.             rsUsers.AddNew
  23.                 rsUsers("name") = LocalVarName 
  24.                 rsUsers("age") = LocalVarAge
  25.                 rsUsers("dateDB") = LocalVarDateDB
  26.             rsUsers.Update
  27.             NewID = rsUsers("idName")
  28.             rsUsers.close
  29.             set rsUsers = nothing
  30.  
  31.         ' Add record into Diagnosis table
  32.             SQL = "insert into Diagnosis (idName, QuestionOne) values (" & NewID & ", '" & LocalVarQuestionOne & "')"
  33.             Conn.Execute(SQL)
  34.  
  35.         Conn.Close
  36.         Set Conn = Nothing
  37.  
  38.         Response.Write "All records were successfully entered into the database."
  39.     End If
  40. %>
  41.  
  42. <html>
  43.     <head>
  44.         <title>Example</title>
  45.     </head>
  46.     <body>
  47.         <form method="post" action="Example.asp" name="xform" id="xform">
  48.             <input type="hidden" name="xSubmit" value="true">
  49.             Name: <input type="text" name="xname"><br>
  50.             Age: <input type="text" name="xage"><br>
  51.             DB Date: <input type="text" name="xdateDB" value="<%=Now()%>"><br>
  52.             Question One: <input type="text" name="xQuestionOne"><br>
  53.             <input type="submit" value="Submit">
  54.         </form>
  55.     </body>
  56. </html>
  57.  
Feb 25 '08 #18
markrawlingson
346 Expert 100+
Sorry, been really busy!! Haven't been able to get around to this yet.

In your code, as quoted below, you haven't passed your SQL anywhere. In fact, it's not even contained within a variable or in quotes! It will most definitely throw an error.

The code provided by CroCrew should do the trick - this is what I meant in my post - sorry guess I should have been more specific :P

Sincerely,
Mark


Hello Experts
I followed Mark's advise and I got this message: I am attaching my access database (I stripped all the unnecessary parts from it).
__________________________________________________ _
Microsoft VBScript compilation error '800a03fd'

Expected 'Case'

/2000040101/form_ac.asp, line 33
Expand|Select|Wrap|Line Numbers
  1. SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName
  2. -------^
  3. __________________________________________________________________
  4. <%
  5. ' Declaring variables
  6. Dim name, age, dateDB, QuestionOne, data_source, con, sql_insert, rs
  7.  
  8. ' A Function to check if some field entered by user is empty
  9. Function ChkString(string)
  10. If string = "" Then 
  11. string = " "
  12. End If
  13. ChkString = Replace(string, "'", "''")
  14. End Function
  15.  
  16. ' Receiving values from Form
  17. name = ChkString(Request.Form("name"))
  18. age = ChkString(Request.Form("age"))
  19. dateDB = ChkString(Request.Form("dateDB"))
  20. QuestionOne = ChkString(Request.Form("QuestionOne"))
  21.  
  22. Set Conn = Server.CreateObject("ADODB.Connection")
  23. Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb") 
  24.  
  25.  
  26. SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName 
  27.  
  28.  
  29.  
  30. rs.Open SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "')" 
  31. Conn.Execute(SQL)
  32.  
  33. rs.Open SQL = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "')" 
  34.  
  35. Conn.Execute(SQL)
  36.  
  37.  
  38. Conn.Close
  39. Set Conn = Nothing
  40.  
  41. Response.Write "All records were successfully entered into the database."
  42. %>
Many thanks for any help.
Debbie
Feb 26 '08 #19

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
7
by: David Bear | last post by:
I have a dictionary that contains a row of data intended for a data base. The dictionary keys are the field names. The values are the values to be inserted. I am looking for a good pythonic...
3
by: MP | last post by:
Hi Posted this several hours ago to another ng but it never showed up thought i'd try here. using vb6, ado, .mdb, jet4.0, no access given table tblJob with field JobNumber text(10) 'The...
2
by: speralta | last post by:
My tired old eyes may be failing me, but the following insert statements look correct to me, but I can't seem to get a clean insert from a fairly large text file database into mysql. I was...
5
by: mabond | last post by:
Hi VB.NET 2005 Express edition Microsoft Access 2000 (SP-3) Having trouble writing an "insert into" command for a Microsoft table I'm accessing through oledb. I've tried to follow the same...
2
by: technocraze | last post by:
Hi guys, I have encountered this error when updating the values to the MS Acess table. Error : Update on linked table failed. ODBC sql server error Timeout expired. MS Acess is my front end and...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
2
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.