Avatar of simonfinn
simonfinn
 asked on

Returning a value to VBA Variable

Hi

Im executing an Insert SQL statment (using DoCMD.RunSQL) into an Access database from VBA. The table that it inserts the data into has an Autonumber field.

I would like the database to return the value of the autonumber field and to store it in a variable in VBA

How can i do this?

Thanks Si


Microsoft Access

Avatar of undefined
Last Comment
simonfinn

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
obyapka

I have not tried to return an autonumber from a SQL INSERT statement, but when you do an ADO or DAO AddNew and assign a variable to a field, the autonumber is then available. ie

.
.
rs.AddNew
rs!MyField = "something"
.
.
iNumber = rs!myAutoNumber
.
.
rs.Update
.
.
regards

Brian
simonfinn

ASKER
Thanks ryancys

Sorry Brian - just a few mins too late
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck