Link to home
Start Free TrialLog in
Avatar of wdarnellg
wdarnellgFlag for United States of America

asked on

Insert to Table Then Update A Different One

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])
     VALUES
           (@RegID, @Attended, @Duration, @Comments)
	DECLARE @idn int
	SET @idn=scope_identity()


UPDATE pr
   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

Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

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.

;=-)

JeffCoachman
Avatar of wdarnellg

ASKER

Thanks for your response Jeff. I have followed your suggestion.
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.AddNew
    rs!c = "abc"
    newid = rs!id
    rs.update

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.





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.
Good points, vadimrapp1!
@vadimrapp1,
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.
> 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.

Thank you everyone. I will get back working tonight as soon as I am back at my machine and get back to you.
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...

2010-Macro.txt
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
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?
currentproject.connection.execute "mystoredprocedure 'string-parm',number-parm "

usually you will simply concatenate:

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




...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.
A trigger seems like a good idea. I have never created one. Can I create one through Management Studio?
ASKER CERTIFIED SOLUTION
Avatar of Vadim Rapp
Vadim Rapp
Flag of United States of America 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