473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to loop an append query

137 100+
Situation:

When a user 'confirms' that they want to save the information they've inputted, I need to append the database records from a temporary table to a different table.

Problem:

The data spans across 3 related tables - scripts, tests and data. One script can have many tests, and one test can have many data entries. When the records are appended, they're also assigned new primary keys, which means the relationships needed to be re-made.

In another instance I've successfully implemented this on a one by one basis, where only one record has been appended per click of the save/confirm button. What I've done is an append query for the main table and an append query for the sub table. Then I've done a DLookup with suitable criteria to find the new primary key of the main record that was just appended, and use an update query on the sub record to fill in the foreign key, thus re-creating the relationship.

However what I don't know is how I can do this when more than one main record is appended at the same time. Obviously an append query does more than one record at a time, but the problem lies in 'capturing' the new primary key for that record so it can be used to update the foreign key for the sub table records.

Does anyone have any ideas of how I could achieve what I'm after? I was wondering if there was a way of creating a loop function that appended one record, sorted it out, and then moved onto the next. All records in the temporary table belonging to the current user will be moved, therefore would it be possible to have a function to do something like:

- A DMin function that finds the first record for that user.
- Append Query to move that record.
- VBA to capture the new primary key (possibly in an invisible textbox)
- Append Query to move the sub-records.
- Update Query to update the relationship between the main record and sub-records.
- Delete Query to delete the record from the temp table.
- Loop back round for any other records present in the temp table.

Is this possible? Or perhaps a better method that doesn't seem like such a hack? Or perhaps am I finally going crazy?

Any suggestions would be much appreciated!

Thanks.

Adam.
Mar 28 '11 #1

✓ answered by Adam Tippelt

For anyone that might be interested, the solution I came up with was:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2. Dim db As dao.Database
  3. Dim rs As dao.Recordset
  4. Dim currentTS As Integer
  5. Dim currentRef As Integer
  6.  
  7.                 Set db = CurrentDb
  8.                 Set rs = db.OpenRecordset("TestsForScripts_temp")
  9.  
  10.                 'Finds the new Script_ID of the latest script that the user appended to the ScriptLibrary table.
  11.                 Me.Script_temp = DMax("Script_ID", "ScriptLibrary", "[ScriptName]='" & Me.ScriptName & "'" & " AND " & _
  12.                                                    "[Client]='" & Me.Client & "'" & " AND " & "[CreatedBy]='" & Me.CreatedBy & "'" & _
  13.                                                    " AND " & "[LastEditedBy]='" & Me.LastEditedBy & "'")
  14.                 'Loops through each test in the script, appends the test and any data requirements, and recreates the relationship between script to test and test to data.
  15.                 Do While Not rs.EOF
  16.                     currentTS = DMin("TS_ID", "TestsForScripts_temp", "[Script_ID]=" & Me.Script_ID) 'Capture the TS_ID of the first found test still in the temp script.
  17.                     currentRef = DLookup("Ref_ID", "TestsForScripts_temp", "[Script_ID]=" & Me.Script_ID & " AND " & "[TS_ID]=" & currentTS) 'Captures the Ref_ID to use later to find the record in the new table.
  18.  
  19.                     'Appends the test to the TestsForScripts table.
  20.                     strSQL = "INSERT INTO TestsForScripts ( Ref_ID, PageTitle, PageAddress, Client, [Field/Element], TestFor, ExpectedResult, TypeOfTest, Comments, DateAdded, AddedBy )" & _
  21.                         "SELECT TestsForScripts_temp.Ref_ID, TestsForScripts_temp.PageTitle, TestsForScripts_temp.PageAddress, TestsForScripts_temp.Client, TestsForScripts_temp.[Field/Element], TestsForScripts_temp.TestFor, TestsForScripts_temp.ExpectedResult, TestsForScripts_temp.TypeOfTest, TestsForScripts_temp.Comments, TestsForScripts_temp.DateAdded, [TestsForScripts_temp].[AddedBy]" & _
  22.                         "FROM [TestsForScripts_temp] " & _
  23.                         "WHERE [TestsForScripts_temp].[TS_ID]=" & currentTS
  24.                     DoCmd.RunSQL strSQL
  25.  
  26.                     'Appends any data requirements for this test to the DataForScripts table.
  27.                     MySQL = "INSERT INTO DataForScripts ( Data_ID, Data_Requirement, Ref_ID, DateAdded, AddedBy)" & _
  28.                         "SELECT DataForScripts_temp.Data_ID, DataForScripts_temp.Data_Requirement, DataForScripts_temp.Ref_ID, DataForScripts_temp.DateAdded, [DataForScripts_temp].[AddedBy]" & _
  29.                         "FROM [DataForScripts_temp] " & _
  30.                         "WHERE [DataForScripts_temp].[TS_ID]=" & currentTS
  31.                     DoCmd.RunSQL MySQL
  32.  
  33.                     'Uses Script_temp to re-create the relationship between the script and the test.
  34.                     DoCmd.OpenQuery "Script_TS_RelationshipQuery"
  35.                     'Finds the new TS_ID of the current test that was appended to the TestsForScripts table.
  36.                     Me.TS_temp = DLookup("TS_ID", "TestsForScripts", "[Script_ID]=" & Me.Script_temp & " AND " & "[Ref_ID]=" & currentRef)
  37.                     DoCmd.OpenQuery "TS_DS_RelationshipUpdateQuery" 'Uses TS_temp to re-create the relationship between the test and any data requirements it has.
  38.  
  39.                     'Delete that test from the temp table now that it's been added to the TestsForScripts table.
  40.                     'Referential integrity means that any data requirements are deleted as well.
  41.                     strSQL = "DELETE * " & _
  42.                         "FROM [TestsForScripts_temp] " & _
  43.                         "WHERE [TestsForScripts_temp].[TS_ID]=" & currentTS
  44.                     DoCmd.RunSQL strSQL
  45.  
  46.                     rs.MoveNext 'This is used to let the system decide if EOF = true, and whether the loop needs to be run again.
  47.                     Loop
  48.  
  49.                 'Delete the empty temp script once all the tests have been individually appended.
  50.                 strSQL = "DELETE * " & _
  51.                     "FROM [ScriptLibrary_temp] " & _
  52.                     "WHERE [ScriptLibrary_temp].[CreatedBy]='" & Me.CreatedBy & "'"
  53.                 DoCmd.RunSQL strSQL
  54.  
  55.                 DoCmd.Close 'Close the form and open the main menu that corresponds to the current user's user type.

4 6768
hype261
207 100+
Adam,

I have done this before using a loop in VBA. Access has a query that will return you the Primary Key of the last transaction that was completed by the user. The syntax is
Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("SELECT @@IDENTITY AS LastID;")
  2.  
So psuedo code for the loop would be
Expand|Select|Wrap|Line Numbers
  1. While some criteria
  2.    Append main record
  3.    get main record ID
  4.    Append Child Records
  5.  
  6. Wend
  7.  
Mar 28 '11 #2
Adam Tippelt
137 100+
Mmm that doesn't really offer any help on what I've already got. It needs to capture the ID number on every iteration of the loop, not just one ID before it.

Now that I've had some sleep and my brain is working again, I've had another go at coding in what I'm after - however I've come up against an error I don't understand:


Run-time error '2465':

Microsoft Office Access can't find the field 'AddedBy' referred to in your expression.


It's erroring on the SQL append query I wrote, yet I can't understand why - the field exists both in the table it's in now, and the table it's being appended to.

Any ideas why it's now producing that error?
Mar 29 '11 #3
Adam Tippelt
137 100+
Mmm figured that one out, onto the next.

I'll post the code if I get it working...and yes...I do realise that I've spent most of this thread talking to myself... :)
Mar 29 '11 #4
Adam Tippelt
137 100+
For anyone that might be interested, the solution I came up with was:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2. Dim db As dao.Database
  3. Dim rs As dao.Recordset
  4. Dim currentTS As Integer
  5. Dim currentRef As Integer
  6.  
  7.                 Set db = CurrentDb
  8.                 Set rs = db.OpenRecordset("TestsForScripts_temp")
  9.  
  10.                 'Finds the new Script_ID of the latest script that the user appended to the ScriptLibrary table.
  11.                 Me.Script_temp = DMax("Script_ID", "ScriptLibrary", "[ScriptName]='" & Me.ScriptName & "'" & " AND " & _
  12.                                                    "[Client]='" & Me.Client & "'" & " AND " & "[CreatedBy]='" & Me.CreatedBy & "'" & _
  13.                                                    " AND " & "[LastEditedBy]='" & Me.LastEditedBy & "'")
  14.                 'Loops through each test in the script, appends the test and any data requirements, and recreates the relationship between script to test and test to data.
  15.                 Do While Not rs.EOF
  16.                     currentTS = DMin("TS_ID", "TestsForScripts_temp", "[Script_ID]=" & Me.Script_ID) 'Capture the TS_ID of the first found test still in the temp script.
  17.                     currentRef = DLookup("Ref_ID", "TestsForScripts_temp", "[Script_ID]=" & Me.Script_ID & " AND " & "[TS_ID]=" & currentTS) 'Captures the Ref_ID to use later to find the record in the new table.
  18.  
  19.                     'Appends the test to the TestsForScripts table.
  20.                     strSQL = "INSERT INTO TestsForScripts ( Ref_ID, PageTitle, PageAddress, Client, [Field/Element], TestFor, ExpectedResult, TypeOfTest, Comments, DateAdded, AddedBy )" & _
  21.                         "SELECT TestsForScripts_temp.Ref_ID, TestsForScripts_temp.PageTitle, TestsForScripts_temp.PageAddress, TestsForScripts_temp.Client, TestsForScripts_temp.[Field/Element], TestsForScripts_temp.TestFor, TestsForScripts_temp.ExpectedResult, TestsForScripts_temp.TypeOfTest, TestsForScripts_temp.Comments, TestsForScripts_temp.DateAdded, [TestsForScripts_temp].[AddedBy]" & _
  22.                         "FROM [TestsForScripts_temp] " & _
  23.                         "WHERE [TestsForScripts_temp].[TS_ID]=" & currentTS
  24.                     DoCmd.RunSQL strSQL
  25.  
  26.                     'Appends any data requirements for this test to the DataForScripts table.
  27.                     MySQL = "INSERT INTO DataForScripts ( Data_ID, Data_Requirement, Ref_ID, DateAdded, AddedBy)" & _
  28.                         "SELECT DataForScripts_temp.Data_ID, DataForScripts_temp.Data_Requirement, DataForScripts_temp.Ref_ID, DataForScripts_temp.DateAdded, [DataForScripts_temp].[AddedBy]" & _
  29.                         "FROM [DataForScripts_temp] " & _
  30.                         "WHERE [DataForScripts_temp].[TS_ID]=" & currentTS
  31.                     DoCmd.RunSQL MySQL
  32.  
  33.                     'Uses Script_temp to re-create the relationship between the script and the test.
  34.                     DoCmd.OpenQuery "Script_TS_RelationshipQuery"
  35.                     'Finds the new TS_ID of the current test that was appended to the TestsForScripts table.
  36.                     Me.TS_temp = DLookup("TS_ID", "TestsForScripts", "[Script_ID]=" & Me.Script_temp & " AND " & "[Ref_ID]=" & currentRef)
  37.                     DoCmd.OpenQuery "TS_DS_RelationshipUpdateQuery" 'Uses TS_temp to re-create the relationship between the test and any data requirements it has.
  38.  
  39.                     'Delete that test from the temp table now that it's been added to the TestsForScripts table.
  40.                     'Referential integrity means that any data requirements are deleted as well.
  41.                     strSQL = "DELETE * " & _
  42.                         "FROM [TestsForScripts_temp] " & _
  43.                         "WHERE [TestsForScripts_temp].[TS_ID]=" & currentTS
  44.                     DoCmd.RunSQL strSQL
  45.  
  46.                     rs.MoveNext 'This is used to let the system decide if EOF = true, and whether the loop needs to be run again.
  47.                     Loop
  48.  
  49.                 'Delete the empty temp script once all the tests have been individually appended.
  50.                 strSQL = "DELETE * " & _
  51.                     "FROM [ScriptLibrary_temp] " & _
  52.                     "WHERE [ScriptLibrary_temp].[CreatedBy]='" & Me.CreatedBy & "'"
  53.                 DoCmd.RunSQL strSQL
  54.  
  55.                 DoCmd.Close 'Close the form and open the main menu that corresponds to the current user's user type.
Mar 30 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John | last post by:
Hi - I am trying to perform a simple append query, with no luck. I have a table (MktPrices) that has the following fields: BondID, PriceDate, Price. The objective is to allow the user to input a...
2
by: JMCN | last post by:
hi i have a general question regarding append queries in access 97. each week i need to update my table(tblonlinereg) with new or modified records. firstly, i import the text file into my...
1
by: Gregg | last post by:
I created an append query that pushed 100's of orders form one table into another. I'd like to assign a new order number to each order as I append them to the new table based upon the maximum...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
4
by: robboll | last post by:
When I try to use an append query from an oracle link it takes forever. I am exploring the idea of doing an append action using a pass-through query. If I have an Oracle ODBC connection to...
4
by: pmacdiddie | last post by:
I have an append query that needs to run every time a line item is added to a subform. The append writes to a table that is the source for a pull down box. Problem is that it takes 5 seconds to...
10
by: Dan2kx | last post by:
Still duin the holiday database... and i have discovered something that i cant explain... can you guys? ok i have a procedure which basically (quite a longwinded process) selects the start date...
4
by: Scott12345 | last post by:
Hi, here is my situation, I have a DB that tracks machine downtime (30 machines) per day. Several users will update this through the day. I created an append query that creates 30 dummy values and...
4
by: franc sutherland | last post by:
Hello, I am using Access 2003. I am having trouble trapping the "can't append all the records in the append query" error message when appending data to a query from a table which is linked to...
2
by: bmac | last post by:
I would like to know if I can force an append query in Access VBA to continue if it encounters a field in the source table that does not exist in the target table and just ignore that field. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.