update table from current form

S

srob

I have a table "customer" which has a one-to-many relationship with
table "addresses". The primary key of "customer" is an autonumber
field. I have a form that displays the customers, and allows you to
add a new customer. Also on this form is a command button that uses
VBA code to add additional addresses for the current customer. My
problem is that when I create a new customer and immediately try to add
addresses, the VBA code fails. If I move back a record and then
forward a record on the form then the VBA code works for the newly
added customer.
I think I need to update the "customer" table on the form when I exit
the first field. How can I do this?
Interestingly, in my VBA code, the line

CustomerID = Forms![customer]!custormerID

returns the correct customer ID of the just added customer, however
when I try to update the address table with;

With rst_addressess
.AddNew ' Add new record.
!customerID = CustomerID ' Add data.
.Update
end with

I get an error when I try to execute the .Update statement.
 
B

Brian Bastl

srob,

You need to explicitly save the record first in code, or by moving off of
the current record (which is what you're doing). Using the On Click event
behind your command button, you can use either:

if me.dirty Then
me.dirty =false
End if

or

DoCmd.RunCommand acCmdSaveRecord

and then your code to open the form

HTH,
Brian
 

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

Top