Link to home
Start Free TrialLog in
Avatar of joop123456
joop123456

asked on

Copying a number to a table when adding new record

Hi,

Have next 2 - related - tables:

t1_AUTOID (autonumber, related to t2)
t1_base_number
t1_etc

and

t2_AUTOID
t1_autoid_rel  (related one-many with t1)
t2_new_number
t2_etc

Now, when adding a new related record to t2, I want the latest t1_base_number to be copied to t2_new_number.
t1_base_number changes all the time, but when I've added a new t2-record, after that the t2_new_number shouldn't change anymore.
So just copy the latest version t1 number to the t2 field.

Prefable no SP... but in a view?  VB-code in Access front-end is ok as well..
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Form an Access Front end, you can use VBA in your form's After Insert event:

Dim strSQL as string
dim lngMaxID as Long
lngMaxID = DMax("t1_AutoID", "t1")

strSQL = "UPDATE T2 SET t2_new_number = " & Dlookup("t1_base_number", "t1", "T1_AUTOID = "  & lngMaxID)
Currentdb.execute strSQL, dbFailOnError
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joop123456
joop123456

ASKER

Hi mbizup!

Thanks!
Have a new situation here as well, just slightly different:

https://www.experts-exchange.com/questions/22896877/Copying-a-number-to-a-table-when-adding-new-record-2.html