Question : How do I access Autonum Id associated with record added to table?

I have several forms which the user can go back and forth between them. The first form has Company info and its record source is CompanyInfo. The user fills in this info, and then can advance to the next form, which has a different record source, but uses the CompanyID as its Foreign Key.

When the user clicks the command button to advance to the next form, I would like the company information record to be added to CompanyInfo and I want to retrieve the autonum assigned to the record so that it can be used in subsequent forms. Then, I want the user to be able to return to the first form, if necessary, so the company info for this same record can be updated, if need be. My 2 questions are:
1) What is the VBA code for adding the record (I'm wasn't thinking about INSERT INTO). In Access 2003, when creating a command button for adding a record, the VBA code would be created. Now, in Access 2007, it only creates a macro, but I need the VBA code.
2) How do I access the autonum Id assigned to the record added.

Thanks.

Answer : How do I access Autonum Id associated with record added to table?

for #1. create a bound form. You don't need any codes to save the record.
< Now, in Access 2007, it only creates a macro, but I need the VBA code>
Office button >access options > object designers > form/report design
  check always use event procedures

#2 . you can open the next form with the openArgs option to pass  the AutoNumID
   docmd.openform "formX", openargs:=me.AutoNumID

in the load event of formX, place this code
private sub form_load()
if len(me.openargs & "")>0 then
   me.txtForeignKey=me.openargs
end if
end sub



Random Solutions  
 
programming4us programming4us