473,799 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding records to a table based on the value of one field of other

My question is what will be the fast algorithm to add records in a
table based on the value of a field of other table.

For example:
TABLE2 contains two fields ID and DESCRIPTION
TABLE1 contains three fields ID, DESCRIPTION and TOTALNUMBER
Then, how to create so many rows in TABLE2 as the value of
TABLE1.TOTALNUM BER

If
TABLE1
------------
ID DESCRIPTION TOTALNUMBER
1 apples 5
2 oranges 4

then TABLE2
--------------------
ID DESCRIPTION
1 apples
1 apples
1 apples
1 apples
1 apples
2 oranges
2 oranges
2 oranges
2 oranges

I propose one, however I'm not sure if it is the fastest way (even if
it is fine).

--------------------------- CODE
-------------------------------------------------------------
conn.Connection String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=" & sPathBase & ";"

Set conn = New ADODB.Connectio n
Set rs1 = New ADODB.Recordset
rs1.Open "SELECT * FROM Table1 ", conn, adOpenStatic, adLockOptimisti c
rs1.movefirst
Do while not rs1.EOF

id = rs1("ID")
description= rs1("DESCRIPTIO N")
sql="INSERT INTO Table2 VALUES (" & id & ",'" & description & "')"
conn.execute(sq l)
rs1.movenext
Loop

set conn = nothing
------------------------ END CODE
------------------------------------------------------------------

Thank you.

Mar 9 '07 #1
4 1781
I'm not even sure what valid purpose would be served by doing so. From your
example, it seems to be the work of the Department of Redundancy
Department -- that is, creating a number of redundant records. But, then,
there might be a purpose in "real life" when you aren't obscuring it with
"sample data".

Larry Linson
Microsoft Access MVP
<ca******@gmail .comwrote in message
news:11******** **************@ v33g2000cwv.goo glegroups.com.. .
My question is what will be the fast algorithm to add records in a
table based on the value of a field of other table.

For example:
TABLE2 contains two fields ID and DESCRIPTION
TABLE1 contains three fields ID, DESCRIPTION and TOTALNUMBER
Then, how to create so many rows in TABLE2 as the value of
TABLE1.TOTALNUM BER

If
TABLE1
------------
ID DESCRIPTION TOTALNUMBER
1 apples 5
2 oranges 4

then TABLE2
--------------------
ID DESCRIPTION
1 apples
1 apples
1 apples
1 apples
1 apples
2 oranges
2 oranges
2 oranges
2 oranges

I propose one, however I'm not sure if it is the fastest way (even if
it is fine).

--------------------------- CODE
-------------------------------------------------------------
conn.Connection String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=" & sPathBase & ";"

Set conn = New ADODB.Connectio n
Set rs1 = New ADODB.Recordset
rs1.Open "SELECT * FROM Table1 ", conn, adOpenStatic, adLockOptimisti c
rs1.movefirst
Do while not rs1.EOF

id = rs1("ID")
description= rs1("DESCRIPTIO N")
sql="INSERT INTO Table2 VALUES (" & id & ",'" & description & "')"
conn.execute(sq l)
rs1.movenext
Loop

set conn = nothing
------------------------ END CODE
------------------------------------------------------------------

Thank you.

Mar 10 '07 #2
On 10 mar, 06:28, "Larry Linson" <boun...@localh ost.notwrote:
I'm not even sure what valid purpose would be served by doing so. From your
example, it seems to be the work of the Department of Redundancy
Department -- that is, creating a number of redundant records. But, then,
there might be a purpose in "real life" when you aren't obscuring it with
"sample data".

Larry Linson
Microsoft Access MVP
Your answer was very helpful,
now I realize why you are MVP ;)

Of course there is a "real life" purpose, however it is not the main
issue in this thread (and forum).
Thank you again.

Mar 14 '07 #3
ca******@gmail. com wrote in news:1173469137 .480177.256350
@v33g2000cwv.go oglegroups.com:
My question is what will be the fast algorithm to add records in a
table based on the value of a field of other table.

For example:
TABLE2 contains two fields ID and DESCRIPTION
TABLE1 contains three fields ID, DESCRIPTION and TOTALNUMBER
Then, how to create so many rows in TABLE2 as the value of
TABLE1.TOTALNUM BER

If
TABLE1
------------
ID DESCRIPTION TOTALNUMBER
1 apples 5
2 oranges 4

then TABLE2
--------------------
ID DESCRIPTION
1 apples
1 apples
1 apples
1 apples
1 apples
2 oranges
2 oranges
2 oranges
2 oranges

I propose one, however I'm not sure if it is the fastest way (even if
it is fine).

--------------------------- CODE
-------------------------------------------------------------
conn.Connection String = _
"Provider=Micro soft.Jet.OLEDB. 4.0;" & _
"Data Source=" & sPathBase & ";"

Set conn = New ADODB.Connectio n
Set rs1 = New ADODB.Recordset
rs1.Open "SELECT * FROM Table1 ", conn, adOpenStatic, adLockOptimisti c
rs1.movefirst
Do while not rs1.EOF

id = rs1("ID")
description= rs1("DESCRIPTIO N")
sql="INSERT INTO Table2 VALUES (" & id & ",'" & description & "')"
conn.execute(sq l)
rs1.movenext
Loop

set conn = nothing
------------------------ END CODE
------------------------------------------------------------------

Thank you.
I think the fastest way is likely to be to create a DAO recordset of
Table2 records and to use AddNew and Update in keeping with the values
found in Table1. In my experience, this is a case in which DAO is not
just fast, but blows the sox off ADO and/or JET SQL. If there are a bunch
of new records to create I'd guess this way might be ten or more times
faster than the fastest other way.
BTW, I use ADO wherever DAO or SQL does not have a clear advantage; in
this case I think the advantage is overwhelming.

--
Mar 14 '07 #4
Thank you lyle

Mar 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2416
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1 abc 2 abc 3
3
11108
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I have a primary form named TestResults, which is connected to data in a table named TestResults. There are basically two other tables that are related to the TestResults table (and the primary form) named Names-Normalized and SiteAddresses. The...
2
2518
by: Robin S. | last post by:
This is an "Add product" form. The user will enter a ProductNo (catalog number), select a Product Class (from cascading combo boxes) and then click a button to create the product. When a product is added, a recordset (class) from a table (tblClassDetails - fields are: ClassID, SpecID) will be cloned into another table (tblClassProJoin - fields are ClassID, ProductID, SpecID and Value). Obviously, ClassID and SpecID will be cloned.
6
2509
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new products). Tables: tblCategoryDetails CategoryID SpecID
6
3077
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a record such as: part number 202354-001 location C1-01
3
4887
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
6
4434
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire row doesn't get filled. I have a connection and all that stuff. Private Sub btnPlaceBet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlaceBet.Click ' Dim Myds As Footbet.DStable '...
16
3502
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown list and checkbox so that they can make changes. I named these elements as arrays and wish to run an...
1
2021
damonreid
by: damonreid | last post by:
Access 2003 Microsoft Windows XP Pro Hey, I am currently pulling my hair out here. I have a Form for adding new projects to a database, the only problem is that when I close the form it doesn't insert that information into the table. This is a major problem as it is suppose to use the information to populate several other tables with key information and open another form based on some of that information (in this case project number). ...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.