avivak1
asked on
Characters found after end of SQL statement.
Hey,
It's 3 oclock in the Am and I'm soo tired! Someone put me out of my misery.....
Here's the question - I wanted to add a Record to a table and then take the newest added record's id number for future reference. The Database is Access.
The problem is I only have the code for adding to a sql server database.
Sample Code Below:
strSql = "insert into atable(field1,field2,field 3,field4) values ('value','value,'value','v alue');sel ect @@IDENTITY ;"
I get this error:[Microsoft][ODBC Microsoft Access Driver] Characters found after end of SQL statement.
I have a feeling it doesn't work because Access doesn't allow multiple sql queries...
Can someone give me sample code that does the same thing for Access with code protecting me that I only get the newest id that was just entered using the insert statement
Thanks,
Aviva
It's 3 oclock in the Am and I'm soo tired! Someone put me out of my misery.....
Here's the question - I wanted to add a Record to a table and then take the newest added record's id number for future reference. The Database is Access.
The problem is I only have the code for adding to a sql server database.
Sample Code Below:
strSql = "insert into atable(field1,field2,field
I get this error:[Microsoft][ODBC Microsoft Access Driver] Characters found after end of SQL statement.
I have a feeling it doesn't work because Access doesn't allow multiple sql queries...
Can someone give me sample code that does the same thing for Access with code protecting me that I only get the newest id that was just entered using the insert statement
Thanks,
Aviva
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The IDENTITY (ID) that it gets is the last record inserted on YOUR current connection so executing it directly after you INSERT statement gets the ID of that record - its full proof.
ASKER
so something like commitTrans, rollbacktrans beginTrans isn't relevant?
That I couldn't answer - but my understanding would be that if you were using MTS and it failed on the insert it wouldn't perform the SELECT @@INDENTITY anyway.
CommitTrans still works but if it is only 1 sql statement, dun think you need it. Just put in an error control trap will do.
>> I have a feeling it doesn't work because Access doesn't allow multiple sql queries...
That is exactly what is going on. Unlike SQL Server, Access can accept only a single query at a time.
That is exactly what is going on. Unlike SQL Server, Access can accept only a single query at a time.
ASKER
standard if statement for error control?
Ok, I have very limited (basically none) exposure to MTS but the following is approximately what you would need to ensure (absolutely) that nothing goes wrong.
<%
Dim objCon, objCmd
Dim rsChck
Dim sQuery
Dim nAffected
Set objCon = Server.CreateObject("ADODB .Connectio n")
objCon.Open "PROVIDER=Microsoft.Jet.OL EDB.4.0;Da ta Source=" & Request.ServerVariables("A PPL_PHYSIC AL_PATH") & "\Misc\DataDB.mdb"
Set objCmd = Server.CreateObject("ADODB .Command")
Set objCmd.ActiveConnection = objCon
sQuery = "INSERT INTO Table (Field1,Field2,Field3) VALUES (1,2,3)"
objCmd.CommandText = sQuery
objCmd.CommandType = 1
objCon.BeginTrans
objCmd.Execute nAffected
If nAffected > 0 then
objCon.CommitTrans
Set rsChck = Server.CreateObject("ADODB .RecordSet ")
sQuery = "SELECT @@ IDENTITY AS NewID"
rsChck.Open sQuery, objCon
If rsChck.EOF then
objCon.RollbackTrans
Else
Response.Write "New ID: " & rsChck("NewID")
Response.End
End If
Else
objCon.RollbackTrans
Response.Write "Process Failed."
Response.End
End If
%>
<%
Dim objCon, objCmd
Dim rsChck
Dim sQuery
Dim nAffected
Set objCon = Server.CreateObject("ADODB
objCon.Open "PROVIDER=Microsoft.Jet.OL
Set objCmd = Server.CreateObject("ADODB
Set objCmd.ActiveConnection = objCon
sQuery = "INSERT INTO Table (Field1,Field2,Field3) VALUES (1,2,3)"
objCmd.CommandText = sQuery
objCmd.CommandType = 1
objCon.BeginTrans
objCmd.Execute nAffected
If nAffected > 0 then
objCon.CommitTrans
Set rsChck = Server.CreateObject("ADODB
sQuery = "SELECT @@ IDENTITY AS NewID"
rsChck.Open sQuery, objCon
If rsChck.EOF then
objCon.RollbackTrans
Else
Response.Write "New ID: " & rsChck("NewID")
Response.End
End If
Else
objCon.RollbackTrans
Response.Write "Process Failed."
Response.End
End If
%>
ASKER
Thanks :)
ASKER