Unless I am mistaken, that is exactly what I did. Here is the code.
Thanks for the thoughts:
strInsert = "INSERT INTO Contacts ([First Name], [Last Name], Company,
Address, [Address 2], City " & _
", State, [Zip Code], [Phone Number], Fax, [Email
Address], User, ModifiedDate) VALUES (""" & Me.txtFirstName & _
""", """ & Me.txtLastName & """, " & strNullCompany &
", """ & Me.txtAddress & _
""", """ & Me.txtAddress2 & """, """ & Me.txtCity &
""", """ & Me.txtState & _
""", """ & Me.txtZipCode & """, """ &
Me.txtPhoneNumber & """, """ & Me.txtFax & _
""", """ & Me.txtEmailAddress & """, """ &
fOSUserName() & """, #" & Date & "#)"
DoCmd.RunSQL strInsert
Set rst = CurrentDb().OpenRecordset("SELECT @@IDENTITY")
debug.print rst.Fields(0)
this prints 0
Bob Barrows said:
icccapital wrote:
I have been doing some reading, but without success in figuring this
out. How do I group two queries an insert and then an @@Identity so
that @@identity won't return 0, but will return the primary key,
which is an autonumber?
I am running access 2007 and am trying to run this query in response
to a control button being pressed so it is in vba.
Errr....
run the insert query that inserts a single record into the table
that has the autonumber field, and, before doing anything else, open
a recordset using SELECT @@IDENTITY.
Seriously ... it's that simple. If it's not working for you, you
will need to show us how to reproduce your symptom.