Avatar of CaptainGiblets
CaptainGiblets
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Insert into help with autonumber.

i have the attatched code.

When i run the code it creates a new record in tblcustomerdetails, there is also a column in tblcustomerdetails called  AUTO   that is an auto number.  so i dont have a record of it anywhere as it is randomly generated.

i need to refer to this new AUTO created in tblcustomerdetails  in the code below it, so that i can paste that new AUTO into the AUTO field on the second table and still pull the rest of the information off as currently scripted.
CurrentProject.Connection.Execute _
    "INSERT INTO tblcustomerdetails ([Date Recorded], Name, [trading as name], Legalconstitution, salesperson, [type of application], [ai/kyc], [internal status], [Date sent to SMS], [Date Approved / Declined], [Date Dispatched], [Date Installed], [Date Active]) SELECT [Date Recorded], Name, [trading as name], Legalconstitution, salesperson, [type of application], [ai/kyc], [internal status], [Date sent to SMS], [Date Approved / Declined], [Date Dispatched], [Date Installed], [Date Active] FROM tblcustomerdetails WHERE tblcustomerdetails.auto = " & Me!subbasicc.Form!txtauto & ""
CurrentProject.Connection.Execute _
    "INSERT INTO tblcomments ([Auto], [handepay comments], [Streamline Comments], [Post Log], [handepay comments2], [Post Log Comments]) SELECT * FROM tblcustomerdetails WHERE tblcomments.auto = " & Me!subbasicc.Form!txtauto & ""

Open in new window

Microsoft AccessMicrosoft SQL Server

Avatar of undefined
Last Comment
CaptainGiblets

8/22/2022 - Mon
BrandonGalderisi

In your first SQL statement do a "select scope_identity()" (without ") to retrieve the value of the newly inserted data.  Then pull recordset and get the value from your connection.
CaptainGiblets

ASKER
would you be able to post an example for me please?
CaptainGiblets

ASKER
i forgot i had a trigger that was automatically creating the records in the other tables i use, i managed to get the 1st part working with 1 table using the code below, however when it gets to the seconds one it fails saying you cant insert null into the field auto     is there a way to get the Identity_scope() command to run further?


Private Sub Command147_Click()
CurrentProject.Connection.Execute _
"INSERT INTO tblcustomerdetails ([Date Recorded], Name, [trading as name], Legalconstitution, salesperson, [type of application], [ai/kyc], [internal status], [Date sent to SMS], [Date Approved / Declined], [Date Dispatched], [Date Installed], [Date Active]) SELECT [Date Recorded], Name, [trading as name], Legalconstitution, salesperson, [type of application], [ai/kyc], [internal status], [Date sent to SMS], [Date Approved / Declined], [Date Dispatched], [Date Installed], [Date Active] FROM tblcustomerdetails WHERE tblcustomerdetails.auto = " & Me!subbasicc.Form!txtauto & ""
CurrentProject.Connection.Execute _
    "Delete from tblcomments WHERE tblcomments.auto = scope_identity()"
CurrentProject.Connection.Execute _
    "INSERT INTO tblcomments ([Auto], [handepay comments], [Streamline Comments], [Post Log], [handepay comments2], [Post Log Comments]) SELECT scope_identity(), [handepay comments], [Streamline Comments], [Post Log], [handepay comments2], [Post Log Comments] FROM tblcomments WHERE tblcomments.auto = " & Me!subbasicc.Form!txtauto & ""

CurrentProject.Connection.Execute _
    "Delete from tblterminaldetails WHERE tblterminaldetails.auto = scope_identity()"
CurrentProject.Connection.Execute _
    "INSERT INTO tblterminaldetails ([Auto], [MID], [TID], [Terminal], [Network], [FURRENTALOF] ,[MINPER], [Outlet], [MRP Doc]) SELECT scope_identity(), [MID], [TID], [Terminal], [Network], [FURRENTALOF] ,[MINPER], [Outlet], [MRP Doc]FROM tblterminaldetails WHERE tblterminaldetails.auto = " & Me!subbasicc.Form!txtauto & ""

Your help has saved me hundreds of hours of internet surfing.
fblack61
CaptainGiblets

ASKER
sorry forgot to add.

After the Space is where it starts to fail.
ASKER CERTIFIED SOLUTION
Scott McDaniel (EE MVE )

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.
CaptainGiblets

ASKER
worked great, thanks.