Get autonumber value for last modified record?

J

JBHansen

This is probably simpler than I'm trying to make it:

A "Save" button on my form adds a new record to the table called
"tblCommunicationsSent". I would now like to have that same "Save" button
UPDATE the record the user just added, instead of ADDING another record to
the table.

I think if I could get the "ID" value (which is an autonumber field in the
table) for the last modified record I would be on the right track, but I
can't figure out how to get it. Any suggestions? Thank you in advance for
any help!

Here is my code for adding the record:
Dim db As Database
Dim rstCommunications As Recordset
Set db = CurrentDb()
Set rstCommunications = db.openrecordset("tblCommunicationsSent",
dbopendynaset)

With rstCommunications
.AddNew
!CaseID = txtCaseID
!CommunicationType = "Letter"
!CommunicationName = txtLetterName
!Communication = txtLetterText
!CommunicationDate = Date
!CommunicationSentBy = Forms!fmnuMainMenu!txtAgentFirstName
.Update
End With
rstCommunications.Close
 
K

Ken Snell [MVP]

You can move the recordset to the last modified record and then store the ID
value (see MyIDValue line):

Dim db As Database
Dim rstCommunications As Recordset
Set db = CurrentDb()
Set rstCommunications = db.openrecordset("tblCommunicationsSent",
dbopendynaset)

With rstCommunications
.AddNew
!CaseID = txtCaseID
!CommunicationType = "Letter"
!CommunicationName = txtLetterName
!Communication = txtLetterText
!CommunicationDate = Date
!CommunicationSentBy = Forms!fmnuMainMenu!txtAgentFirstName
.Update
.Bookmark = .LastModified
MyIDValue = !ID
End With
rstCommunications.Close
 
M

Marshall Barton

Ken said:
You can move the recordset to the last modified record and then store the ID
value (see MyIDValue line):

Dim db As Database
Dim rstCommunications As Recordset
Set db = CurrentDb()
Set rstCommunications = db.openrecordset("tblCommunicationsSent",
dbopendynaset)

With rstCommunications
.AddNew
!CaseID = txtCaseID
!CommunicationType = "Letter"
!CommunicationName = txtLetterName
!Communication = txtLetterText
!CommunicationDate = Date
!CommunicationSentBy = Forms!fmnuMainMenu!txtAgentFirstName
.Update
.Bookmark = .LastModified
MyIDValue = !ID
End With
rstCommunications.Close


Taking it one step further, for a Jet db AutoNumber field,
you don't even have to move to the new record. The ID field
is available as soon as you dirty the new record.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top