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

insert record and return id

Status
Not open for further replies.

belovedcej

Programmer
Nov 16, 2005
358
US
I am unfamiliar with DAO objects, though I have been reading up on them. I have mostly used ADO (though i'm very new to that, too.). My db is using linked tables to an Access backend.

Anyway, I need to save a record and return the id (unbound form.)

I have basically the following code:

Code:
Public Function saveDonation(my incoming parameters)
Dim strSQL As String
strSQL = my insert statement
          
CurrentDb.Execute strSQL, dbFailOnError

strSQL = "SELECT @@IDENTITY" & ";"
saveDonation = CurrentDb.Execute(strSQL, dbFailOnError)

End Function

The last line will not compile - I get the message that a variable or function is expected, and it breaks on .Execute. However, my record does save, so the problem isn't in my insert statement.

I have tried any number of things, and this is the closest I've gotten. Any ideas? I'm willing to change the approach if necessary, but if I need to work with recordset objects, I have to stick with DAO in this case.

Thanks.
 
Compliments of PHV or Roy Vidar....

something more like this,

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset, SQL As String
Set cn = CurrentProject.Connection
cn.Execute "INSERT INTO tblCountry(...)",,adExecuteNoRecords
Set rs = cn.Execute("select @@identity", , adCmdText)
MsgBox rs.Fields(0).Value
 
Is there a way to do that using DAO instead of ADO?

All the rest of the code is using DAO because after researching it I have found that DAO is faster when using JET.

I wouldn't have had a problem with this, doing ADO, because that's what I normally use.
 
Code:
Public Function saveDonation(my incoming parameters) As Recordset 
Dim strSQL As String
strSQL = my insert statement
          
CurrentDb.Execute strSQL, dbFailOnError

strSQL = "SELECT @@IDENTITY" & ";"
saveDonation = CurrentDb.openrecordset (strSQL, dbFailOnError)

End Function
 
note this line
Code:
saveDonation = CurrentDb.[COLOR=red]openrecordset [/color](strSQL, dbFailOnError)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top