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: -
<%
-
' Declaring variables
-
Dim name, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert, sql_insertOne
-
-
' A Function to check if some field entered by user is empty
-
Function ChkString(string)
-
If string = "" Then string = " "
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
' Receiving values from Form
-
name = ChkString(Request.Form("name"))
-
age = ChkString(Request.Form("age"))
-
dateDB = ChkString(Request.Form("dateDB"))
-
QuestionOne = ChkString(Request.Form("QuestionOne"))
-
-
-
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
-
Server.MapPath("form.mdb")
-
-
sql_insert = "insert into users (name, age, dateDB) values ('" & _
-
name & "', '" & age & "', '" & dateDB & "'); insert into Diagnosis (QuestionOne) values ('" & _ QuestionOne & "')"
-
-
-
' Creating Connection Object and opening the database
-
Set con = Server.CreateObject("ADODB.Connection")
-
-
con.Open data_source
-
con.Execute sql_insert
-
' Done. Close the connection
-
con.Close
-
Set con = Nothing
-
-
-
Response.Write "All records were successfully entered into the database."
-
%>
18 2083
Hi there,
Are you recieving an error when you run your code?
Dr B
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~ -
<%
-
' Declaring variables
-
Dim name, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert, sql_insertOne
-
-
' A Function to check if some field entered by user is empty
-
Function ChkString(string)
-
If string = "" Then
-
string = " "
-
End If
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
' Receiving values from Form
-
name = ChkString(Request.Form("name"))
-
age = ChkString(Request.Form("age"))
-
dateDB = ChkString(Request.Form("dateDB"))
-
QuestionOne = ChkString(Request.Form("QuestionOne"))
-
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb")
-
-
SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "')"
-
Conn.Execute(SQL)
-
SQL = "insert into Diagnosis (QuestionOne) values ('" & _ QuestionOne & "')"
-
Conn.Execute(SQL)
-
-
Conn.Close
-
Set Conn = Nothing
-
-
Response.Write "All records were successfully entered into the database."
-
%>
-
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
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.
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
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: - SQL = "insert into users (name, age, dateDB) values ('" & name & "', '"
-
sql = sql & age & "', '" & dateDB & "');"
-
sql = sql & "insert into Diagnosis (QuestionOne, idName) values ('"
-
sql = sql & QuestionOne & "', (SELECT idName FROM users WHERE name = '"
-
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
Hello Jared,
This is updated code and I am still receiving same error message (idName is autonumber): - <%
-
' Declaring variables
-
Dim name, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert, sql_insertOne
-
-
' A Function to check if some field entered by user is empty
-
Function ChkString(string)
-
If string = "" Then
-
string = " "
-
End If
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
' Receiving values from Form
-
name = ChkString(Request.Form("name"))
-
age = ChkString(Request.Form("age"))
-
dateDB = ChkString(Request.Form("dateDB"))
-
QuestionOne = ChkString(Request.Form("QuestionOne"))
-
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb")
-
-
SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "');"
-
sql = sql & "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "', (SELECT idName FROM users WHERE name = '" & name & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
-
-
Conn.Execute(SQL)
-
-
-
Conn.Close
-
Set Conn = Nothing
-
-
Response.Write "All records were successfully entered into the database."
-
%>
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: - SQL = "insert into users (name, age, dateDB) values ('" & name
-
sql = sql & "', '" & age & "', '" & dateDB & "')"
-
Conn.Execute(SQL)
-
-
sql = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne
-
sql = sql & "', (SELECT idName FROM users WHERE name = '" & name
-
sql = sql & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
-
Conn.Execute(SQL)
Is the error coming from the second execute line?
Jared
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
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
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 - SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName
-
-------^
-
__________________________________________________________________
-
<%
-
' Declaring variables
-
Dim name, age, dateDB, QuestionOne, data_source, con, sql_insert, rs
-
-
' A Function to check if some field entered by user is empty
-
Function ChkString(string)
-
If string = "" Then
-
string = " "
-
End If
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
' Receiving values from Form
-
name = ChkString(Request.Form("name"))
-
age = ChkString(Request.Form("age"))
-
dateDB = ChkString(Request.Form("dateDB"))
-
QuestionOne = ChkString(Request.Form("QuestionOne"))
-
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb")
-
-
-
SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName
-
-
-
-
rs.Open SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "')"
-
Conn.Execute(SQL)
-
-
rs.Open SQL = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "')"
-
-
Conn.Execute(SQL)
-
-
-
Conn.Close
-
Set Conn = Nothing
-
-
Response.Write "All records were successfully entered into the database."
-
%>
Many thanks for any help.
Debbie
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.
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
------------------------------------------------------------------------------ - <%
-
' Declaring variables
-
Dim FullName, age, dateDB, QuestionOne, data_source, con, conOne, sql_insert
-
-
' A Function to check if some field entered by user is empty
-
Function ChkString(string)
-
If string = "" Then
-
string = " "
-
End If
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
' Receiving values from Form
-
name = ChkString(Request.Form("FullName"))
-
age = ChkString(Request.Form("age"))
-
dateDB = ChkString(Request.Form("dateDB"))
-
QuestionOne = ChkString(Request.Form("QuestionOne"))
-
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb")
-
-
sql_insert = "insert into users (FullName, age, dateDB) values ('" & FullName & "', '" & age & "', '" & dateDB & "')"
-
Conn.Execute(sql_insert)
-
-
-
sql_insert = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "', (SELECT idName FROM users WHERE FullName = '" & FullName & "' AND age = '"& age & "' AND dateDB = '"& dateDB & "'))"
-
-
Conn.Execute(sql_insert)
-
-
-
Conn.Close
-
Set Conn = Nothing
-
-
Response.Write "All records were successfully entered into the database."
-
%>
Please help,
Debbie
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.
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
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.
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 -
<%
-
Function ChkString(string)
-
If string = "" Then
-
string = " "
-
End If
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
If (Request.Form("xSubmit")) Then
-
LocalVarName = ChkString(Request.Form("xname"))
-
LocalVarAge = ChkString(Request.Form("xage"))
-
LocalVarDateDB = ChkString(Request.Form("xdateDB"))
-
LocalVarQuestionOne = ChkString(Request.Form("xQuestionOne"))
-
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb")
-
-
' Add record into Users table
-
Set rsUsers = CreateObject("ADODB.Recordset")
-
rsUsers.CursorLocation = 3
-
rsUsers.Open "SELECT TOP 1 * FROM users", Conn, 3, 3
-
rsUsers.AddNew
-
rsUsers("name") = LocalVarName
-
rsUsers("age") = LocalVarAge
-
rsUsers("dateDB") = LocalVarDateDB
-
rsUsers.Update
-
NewID = rsUsers("idName")
-
rsUsers.close
-
set rsUsers = nothing
-
-
' Add record into Diagnosis table
-
SQL = "insert into Diagnosis (idName, QuestionOne) values (" & NewID & ", '" & LocalVarQuestionOne & "')"
-
Conn.Execute(SQL)
-
-
Conn.Close
-
Set Conn = Nothing
-
-
Response.Write "All records were successfully entered into the database."
-
End If
-
%>
-
-
<html>
-
<head>
-
<title>Example</title>
-
</head>
-
<body>
-
<form method="post" action="Example.asp" name="xform" id="xform">
-
<input type="hidden" name="xSubmit" value="true">
-
Name: <input type="text" name="xname"><br>
-
Age: <input type="text" name="xage"><br>
-
DB Date: <input type="text" name="xdateDB" value="<%=Now()%>"><br>
-
Question One: <input type="text" name="xQuestionOne"><br>
-
<input type="submit" value="Submit">
-
</form>
-
</body>
-
</html>
-
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 - SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName
-
-------^
-
__________________________________________________________________
-
<%
-
' Declaring variables
-
Dim name, age, dateDB, QuestionOne, data_source, con, sql_insert, rs
-
-
' A Function to check if some field entered by user is empty
-
Function ChkString(string)
-
If string = "" Then
-
string = " "
-
End If
-
ChkString = Replace(string, "'", "''")
-
End Function
-
-
' Receiving values from Form
-
name = ChkString(Request.Form("name"))
-
age = ChkString(Request.Form("age"))
-
dateDB = ChkString(Request.Form("dateDB"))
-
QuestionOne = ChkString(Request.Form("QuestionOne"))
-
-
Set Conn = Server.CreateObject("ADODB.Connection")
-
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("form.mdb")
-
-
-
SELECT users.form, Diagnosis.form, Type FROM users JOIN Diagnosis ON users.idName = Diagnosis.idName
-
-
-
-
rs.Open SQL = "insert into users (name, age, dateDB) values ('" & name & "', '" & age & "', '" & dateDB & "')"
-
Conn.Execute(SQL)
-
-
rs.Open SQL = "insert into Diagnosis (QuestionOne, idName) values ('" & QuestionOne & "')"
-
-
Conn.Execute(SQL)
-
-
-
Conn.Close
-
Set Conn = Nothing
-
-
Response.Write "All records were successfully entered into the database."
-
%>
Many thanks for any help.
Debbie
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 :...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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...
|
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...
|
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,...
|
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...
| |