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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forum code

Status
Not open for further replies.

exodus300

Programmer
Mar 22, 2002
262
AU
Hi,

I'm trying to make a message forum using ASP. I have two tables in my database:

Code:
Threads (ThreadID, Title, StartedBy, StartedDate, StartedTime)
Posts (PostID, ThreadID, PostedBy, PostedDate, PostedTime, Content)

ThreadID (Threads table) and PostID (Posts table) are the primary keys; both are Autonumbers.

Adding a new post (ie replying to an existing thread) is easy; just add a new record with the appropriate thread ID.

But what I want to do now is create a new thread; ie. add a new record to the Threads table, then, using the Autonumber key from that new record, add the first post to the Posts table.

The bit in italics of the last paragraph is what I am having trouble with. How can I get the ID of the record I just added, so I can use it to add the new record in the other table?

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
well how are u adding the records? are u using rs.addnew or plain insert stmt?

Known is handfull, Unknown is worldfull
 
like this:

Code:
	sql1 = "INSERT INTO Threads (Title, StartedBy, StartedDate, StartedTime) VALUES ('" & fixTitle & "', '" & _
	 Session("UserName") & "', #" & Date() & "#, #" & Time() & "#)"

	Set conn = Server.CreateObject("ADODB.Connection")
	conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("/database/forums.mdb")
	conn.Execute(sql1)

	conn.Close
	Set conn = Nothing

Is there a better way to add records?

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
that is the best way but in that way u cannot get the recenty added new id, but using rs.addnew u can get the recently added id, check it out...

Known is handfull, Unknown is worldfull
 
How do I go about using rs.addnew? Is there a demo somewhere I can look at?

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
OK I used rs.AddNew on an existing, working page using the old method. I changed the code to this:

Code:
<%
	Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
	Set rs = Server.CreateObject(&quot;ADODB.RecordSet&quot;)
	conn.Open &quot;DRIVER=Microsoft Access Driver (*.mdb);DBQ=&quot; & Server.MapPath(&quot;/cokeandrumisyum/database/forums.mdb&quot;)
	rs.Open &quot;Posts&quot;, conn
	
	rs.AddNew
	rs(&quot;ThreadID&quot;) = Request.Form(&quot;ThreadID&quot;)
	rs(&quot;PostedBy&quot;) = Session(&quot;UserName&quot;)
	rs(&quot;PostedDate&quot;) = Date()
	rs(&quot;PostedTime&quot;) = Time()
	rs(&quot;Content&quot;) = Replace(Request.Form(&quot;Content&quot;), &quot;'&quot;, &quot;''&quot;)
	rs.Update
	
	rs.Close
	Set rs = Nothing
	conn.Close
	Set conn = Nothing
%>

However, I get this error:
Code:
Error Type:
ADODB.Recordset (0x800A0CB3)
Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype.
/cokeandrumisyum/forums/reply.asp, line 46

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
ok i think i fixed it...

I changed the rs.Open line to:
Code:
rs.Open &quot;Posts&quot;, conn


Now, back to my original question... after adding the record using the new code above, can I just user rs(&quot;ThreadID&quot;) to get the value in the autonumber field ThreadID?

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
Well I tried to use Response.Write &quot;Thread ID:&quot; & rs(&quot;ThreadID&quot;) before and after the rs.Update line, but it just shows Thread ID: with no number.

How can I get the new thread id?

[Thanks in advance|Hope I helped you]
Exodus300
[pc3]
 
hi, there is a method in recordset itself, i dont rememeber it now but i will look into it and let yu know, pls give me some time...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top