Subject: How To insert auto increment ID to other table
Posted By: ibrhaim Post Date: 12/6/2006 12:26:40 AM
Hi all, I am having problem inserting the auto_increment id (FID) of table "TOPIC" to FTopicID of Table Msgs, I am using mysql as databse.The code works fine with MS Access Databse.Could any body help me.My codes are as follows:-

Dim rsTopic
set rsTopic = Server.CreateObject("ADODB.Recordset")
rsTopic.Open "Topic", objconn, adOpenForwardOnly, adLockOptimistic, adCmdTable

rsTopic.AddNew

If Request.Cookies("PMBVisit")("UserName") = "" Then
rsTopic("FGuest") = Request.Form("FName")
rsTopic("FTProfile") = False
Else
rsTopic("FName") = Request.Cookies("PMBVisit")("UserID")
rsTopic("FTProfile") = True
End If

rsTopic("FForumJoinID") = Request.Querystring("ForumID")
rsTopic("FSubject") = Request.Form("FSubject")
'the problem is here
Session("FTopicID") = rsTopic("FID")

rsTopic("FIcon") = Request.Form("TopicIcon")

rsTopic.Update
rsTopic.Close
set rsTopic = Nothing


Dim rsMsgs
set rsMsgs = Server.CreateObject("ADODB.Recordset")
rsMsgs.Open "Msgs", objConn, adOpenForwardOnly, adLockOptimistic, adCmdTable

rsMsgs.AddNew

If Request.Cookies("PMBVisit")("UserName") = "" Then
rsMsgs("FMsgGuest") = Request.Form("FName")
rsMsgs("FMsgEmail") = Request.Form("FEmail")
rsMsgs("FMsgProfile") = False
Else
rsMsgs("FMsgName") = Request.Cookies("PMBVisit")("UserID")
rsMsgs("FMsgEmail") = Request.Cookies("PMBVisit")("Email")
rsMsgs("FMsgProfile") = True
End If

rsMsgs("FMsg") = Request.Form("FMsg")

rsMsgs("FTopicID") = Session("FTopicID")
rsMsgs("FMsgForumID") = Request.QueryString("ForumID")
rsMsgs("FMsgIP") = Request.ServerVariables("LOCAL_ADDR")

rsMsgs.Update
rsMsgs.Close
set rsMsgs = Nothing



Reply By: ipaine Reply Date: 12/7/2006 8:48:21 AM
What is the error or result your getting?

Isa Paine
Software Developer
Reply By: ibrhaim Reply Date: 12/8/2006 8:55:44 AM
There is no error, but the field which I wanted to update with "FtopicDI" in table msgs is null, I should be equal to the autoincrement "FID" which is in table Topic. rest are working fine.

Reply By: rstelma Reply Date: 12/8/2006 9:31:03 AM
I've had nothing but trouble with the AddNew method in Classic ASP. Executing a SQL statement to update works much better.

Set rsConn = Server.CreateObject("ADODB.Connection")
rsConn.Open "[Connection String]"
SQL = "UPDATE msgs SET Ftopic = '" & [variable] & "';"
rsConn.Execute(SQL)
rsConn.Close
Set rsConn = Nothing

Reply By: dparsons Reply Date: 12/8/2006 1:26:00 PM
First, this is a cross post: http://p2p.wrox.com/topic.asp?TOPIC_ID=53196 and I answered the question there, however, I assumed that the table you were trying to insert data into that the column fTopic was also an auto inc field.  

What happens if you do:

INSERT INTO msgs(Ftopic)Values(" & [variable] &");"

Do you get an error?

Also, are you sure the variable you are trying to reference actually contains a value? Step through your code.

-------------------------
I will only tell you how to do it, not do it for you.  
Unless, of course, you want to hire me to do work for you.

^^Thats my signature

Go to topic 53025

Return to index page 98
Return to index page 97
Return to index page 96
Return to index page 95
Return to index page 94
Return to index page 93
Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89