Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add New Record in Access Database

Status
Not open for further replies.

d1004

Programmer
May 9, 2002
78
US
I try to add a new record into my Access table (OBJECT ), but somehow it didn’t work. The table that I tried to add into has a one to many relationship with another table (LESSON), where LESSON table is one and OBJECT is many. Does that mean I have to reference or write out a join SQL statement to join the tables in my ASP page in order for it to add.
When I delete the relationship, it added okay. But once I connect the table back, the page doesn’t run. Here is my code, and I hope somebody can help me with it. Thank you!!!

<%
Option Explicit
Dim strConnect
%>

<html>
<head>
<title> Add Record </title>
</head>
<body>
<%
Dim objRS, intObjectID, objConn
Dim adOpenDynamic, adLockOptimistic, adCmdTable
adOpenDynamic = 2
adLockOptimistic = 3
adCmdTable = 2


Set objConn = Server.CreateObject (&quot;ADODB.Connection&quot;)
Set objRS = Server.CreateObject (&quot;ADODB.Recordset&quot;)
strConnect = &quot;Driver={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;/db/database.mdb&quot;)&&quot;;&quot; &_
&quot;Persist Security Info = False&quot;

objConn.Open(strConnect)
objRS.open &quot;OBJECT&quot;, objConn, 2, 3, 2

objRS.MoveLast
intObjectID = ObjRS (&quot;ObjectID&quot;) + 1

objRS.AddNew
objRS (&quot;ObjectID&quot;) = intObjectID
objRS (&quot;Entry&quot;) = Request.Form(&quot;entry&quot;)
objRS (&quot;Title&quot;) = Request.Form(&quot;title&quot;)
objRS (&quot;Language&quot;) = Request.Form(&quot;language&quot;)
objRS (&quot;Description&quot;) = Request.Form(&quot;description&quot;)
objRS (&quot;Version&quot;) = Request.Form(&quot;version&quot;)
objRS (&quot;Status&quot;) = Request.Form(&quot;status&quot;)
objRS (&quot;Date&quot;) = Request.Form(&quot;date&quot;)
objRS (&quot;Format&quot;) = Request.Form(&quot;format&quot;)
objRS (&quot;Duration&quot;) = Request.Form(&quot;duration&quot;)
objRS (&quot;InteractivityLevel&quot;) = Request.Form(&quot;interactivityLevel&quot;)
objRS (&quot;EndUserSpecification&quot;) = Request.Form(&quot;endUser&quot;)
objRS (&quot;Difficulty&quot;) = Request.Form(&quot;difficulty&quot;)
objRS (&quot;Copyright&quot;) = Request.Form(&quot;copyright&quot;)
objRS (&quot;Annotation&quot;) = Request.Form(&quot;comment&quot;)
objRS (&quot;Taxon&quot;) = Request.Form (&quot;taxon&quot;)
objRS (&quot;LessonID&quot;) = Request.Form(&quot;lessonID&quot;)
objRS (&quot;LearningObjectiveID&quot;) = Request.Form(&quot;objective&quot;)
objRS (&quot;AuthorID&quot;) = Request.Form(&quot;authorID&quot;)

objRS.Update
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
Response. Redirect &quot;/index.html&quot;
%>

</body>
</html>
 
It sounds like you're trying to add a record into OBJECT that has no parent in LESSON. Make sure Request.Form(&quot;lessonID&quot;) is a valid LessonID in your LESSON table.
To err is human, but to really foul things up requires a computer.
- Farmers' Almanac, 1978

 
I make sure that the record get enter into the LESSON table first before I add a new record into the OBJECT table. But somehow, it wouldn't let me add.
 
are you adding the record to the LESSON table on a separate page, before you call this code? khorjak's explanation sounds reasonable -- i would try debugging by response.writing whatever is in request.form(&quot;lessonID&quot;) instead of performing the insert, run your page, and verify that this lessonID does indeed exist beforehand.

good luck!
 
The LESSON table already have all the records in there. I used the LessonID in the Lesson table as the foreign key in my OBJECT table. Basically, the user that enter the the information will know before hand what LessonID they need to enter in order to match with what is in the LESSON table.
 
Thank you for your suggestion, but I figure it out. There was a typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top