Go Premium for a chance to win a PS4. Enter to Win


Insert to Table Then Update A Different One

Posted on 2010-08-26
Medium Priority
Last Modified: 2013-11-28
I have a peice of code that works on SQL Server. But now I need the same functionality in Access. I need to insert  or update values in child table, capture the new record, then update a value in the parent table. I can't make the sql work as is so can anyone tell me how to modify the statements to work in MS Access?
INSERT INTO [dbo].[RecordedLesson]
           (@RegID, @Attended, @Duration, @Comments)
	DECLARE @idn int
	SET @idn=scope_identity()

   SET [PlayerID] = l.PlayerID  
     , [ModuleId] = l.ModuleId
     , [SignupDate] = l.[SignupDate]
     , [LessonTypeID] = l.[LessonTypeID]
     , [LocationID] = l.LocationID 
     , [Lessons] = l.Lessons 
     , [Paid] = l.Paid 
     , [Used] = ((SELECT rl.[Duration] 
                   FROM [dbo].[RecordedLesson] rl
                 WHERE rl.LesID = @idn) + l.Used
  FROM [dbo].[LessonRegistration] pr
  JOIN [dbo].[LessonRegistration] l  
    ON pr.RegID = l.RegID
 WHERE pr.RegID = @RegID

Open in new window

Question by:wdarnellg
  • 6
  • 5
  • 2
  • +2
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33536274
Click the "Request Attention" button and ask the the S+QL syntax zone be added to this Q.

A lot of Experts there get questions like this, and may be able to help.



Author Comment

ID: 33538959
Thanks for your response Jeff. I have followed your suggestion.
LVL 40

Expert Comment

by:Vadim Rapp
ID: 33541408
Here are two key techniques that will get you on your way.

To insert new record in table1(id autonumber, c char)

    Dim rs As Recordset
    Set rs = CurrentDb.TableDefs("table1").OpenRecordset
    rs!c = "abc"
    newid = rs!id

Note that the autonumbered field id from just-created record is available even before the new record is commited to the database.

The syntax to update table2 from a join with table1:

UPDATE table2 INNER JOIN Table1 ON table2.id = Table1.id SET table2.cc = [table1].[c];

You can assist yourself by creating Query in Access and then either copying its sql, or saving the query and running it by name from the code.

Going even further however, evaluate whether you need the code to begin with. Because Access keeps the data in the same place as presentation layer, maybe you can simply bind the controls on the forms to the respective fields in the tables, so the data will be created and updated automatically without any code at all.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 60

Expert Comment

by:Kevin Cross
ID: 33541411
In MS Access, I believe @@identity works as there is NOT a scope_identity() function.

If you have difficulties with translating anything else in the statements, please advise, but should be pretty standard SQL and for the parameters, I would suspect you are doing this in a VBA procedure|function, so can just use parameters|variables there.
LVL 60

Expert Comment

by:Kevin Cross
ID: 33541421
Good points, vadimrapp1!
LVL 22

Expert Comment

ID: 33542102
If one uses your approach:
  • Is it possible that another user might also generate the same Autonumber value before you commit the record to the database (thus creating issues for one or the other of the users)?
  • What happens if you do not actually commit the record to the database?  I.e. is that Autonumber value lost to the world and a gap will exist in the sequence of UIDs?
The former question may answer the latter but it is also probably of more significance.
LVL 40

Expert Comment

by:Vadim Rapp
ID: 33542381
> Is it possible that another user might also generate the same Autonumber value before you commit the record to the database (thus creating issues for one or the other of the users)?

No, if another user issues addnew after the 1st one issued addnew but before the 1st one has commited the record, 2nd user's new autonumber will be more than the 1st one. However, as you probably know, when there are more than one user, it's highly advisable to use SQL Server. You can try it out by opening 2 instances of Access, opening the same database, and running the same code in debug mode.

>  I.e. is that Autonumber value lost to the world and a gap will exist in the sequence of UIDs?

Maybe not to the world, but to this particular table it is lost. Yes, there will be a gap. Same as if you delete a record from the middle.


Author Comment

ID: 33546533
Thank you everyone. I will get back working tonight as soon as I am back at my machine and get back to you.

Author Comment

ID: 33548245
I feel rather silly as I need further explanation. I am using Access 2010 and my tables are linked to a SQL Server db via an ODBC connection on a website host. I am seeking to use Access functionality on my desktop to update tables on the web. The previous code snippet is from a stored proc that I wish I could simply fire from Access.

I would love to automate this through the controls, but it seems I need at least some Macro assistance.
There are some Macro commands that I can't seem to quite peice together, but it seems they are only available to me if I have local tables, therefore to get started, I converted the needed tables to 'Local'.  I saved this one Macro in notepad / xml showing my attempt to update the second table in the first table's 'After Insert' event. I get an error message that says: "EditRecord failed because the default alias represents a record which is read only" I don't know how to make the alias editable.

I am thinking that the VBA code suggestion belongs with a button click event? I'll start on that now. I am not familiar with VBA syntax, but here goes...

LVL 40

Expert Comment

by:Vadim Rapp
ID: 33548321
If you already have working stored procedure in sql server, the indeed, why converting. Launch it from your Access using pass-through query.

Or, you can create an ADP instead of MDB, which will give direct access to sql server.  http://accessjunkie.com/faq_23.aspx

Author Comment

ID: 33561229
I like the ADP I think. I just can't seem to figure out how to pass the required parameters to the stored procedure. I am trying to use:
DoCmd.RunSQL "EXEC InsertRecordedLesson"

Can you guide me through how to give parameters to the Stored Proc?
LVL 40

Expert Comment

by:Vadim Rapp
ID: 33561452
currentproject.connection.execute "mystoredprocedure 'string-parm',number-parm "

usually you will simply concatenate:

currentproject.connection.execute "mystoredprocedure '" & forms!form1!field1 & "'," & forms!form1!numericfield

LVL 40

Expert Comment

by:Vadim Rapp
ID: 33561551
...though, speaking of the particular stored procedure in the initial post, I think it would be the best to set up a trigger on the table in sql server, so whenever new record is inserted in the table RecordedLesson, the trigger will automatically update the totals in the table LessonRegistration .

Even better solution would be to eliminate the calculated totals in the table LessonRegistration alltogether, and instead create a view with aggregate functions (unless the table RecordedLesson is so large that calculating the totals takes significant time), so the totals are calculated on-the-fly when they are needed. No need to keep redundant data, unless this is is data warehouse.

Author Comment

ID: 33564210
A trigger seems like a good idea. I have never created one. Can I create one through Management Studio?
LVL 40

Accepted Solution

Vadim Rapp earned 2000 total points
ID: 33564305
yes, you can.

create trigger RecordedLesson_U on RecordedLesson for insert as
set nocount on
update LessonRegistration set [used] = [used] + Inserted.duration
from LessonRegistration join Inserted on Inserted.RegId=LessonRegistration.regid

But as I said, even better to drop column [used] from LessonRegistration, rename LessonRegistration to LessonRegistration0, and  and

create view LessonRegistration as
select LessonRegistration0.*, isnull(sum([RecordedLesson.used]),0) as Used from
LessonRegistration0 left join RecordedLesson on LessonRegistration0.regid=RecordedLesson.regid
group by <all columns of LessonRegistration0 >

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question