473,387 Members | 3,810 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,387 software developers and data experts.

Access VBA Loading table by Using Append and Update query of data from 2 other tables

Hey guys!

Thanks in advance for your help. I'm working in loading a table with values from a product table, and then update those products with every customer name, So that every single customer name will be associated with all products. Note: there is not relationship on these tables yet. Below are the table’s contents

PRODUCT TABLE
Prod_id Product
1 orange
2 apple
3 pineapple
4 grape
5 carrots

CUSTOMER TABLE
Cust_ID Customers
1 Peter Smith
2 John Hopkins
3 Carl Fisherman
4 Josehp Perez
5 Kathy Baez
6 Sofia Scarlett

TRANS_CUSTOMER TABLE

Cust_ID Cust_Name Product
1 Peter Smith orange
1 Peter Smith apple
1 Peter Smith pineapple
1 Peter Smith grape
1 Peter Smith carrots
2 John Hopkins orange
2 John Hopkins apple
2 John Hopkins pineapple
2 John Hopkins grape
2 John Hopkins carrots
orange
apple
pineapple
grape
carrots

for customer number 3 will be update into the trans_cust table. Right now I'm doing this one by one, is there any way I can do a loop so I can update every single customer record into the trans_cust table. thanks so much!
Oct 20 '09 #1

✓ answered by ADezii

Here is one Method you can use that will populate the TRANS_CUSTOMER TABLE with every combination of Customer and Product. I'm totally ignoring the fact that the Database needs some Normalization and Re-Structuring:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstCustomer As DAO.Recordset
  3. Dim rstProduct As DAO.Recordset
  4. Dim rstTransCustomer As DAO.Recordset
  5.  
  6. Set MyDB = CurrentDb
  7. Set rstCustomer = MyDB.OpenRecordset("CUSTOMER TABLE", dbOpenForwardOnly)
  8. Set rstProduct = MyDB.OpenRecordset("PRODUCT TABLE", dbOpenSnapshot)
  9. Set rstTransCustomer = MyDB.OpenRecordset("TRANS_CUSTOMER TABLE", dbOpenDynaset)
  10.  
  11. Do While Not rstCustomer.EOF
  12.   Do While Not rstProduct.EOF
  13.     With rstTransCustomer
  14.       .AddNew
  15.         ![Cust_id] = rstCustomer![Cust_id]
  16.         ![Cust_Name] = rstCustomer![Customers]
  17.         ![Product] = rstProduct![Product]
  18.       .Update
  19.     End With
  20.       rstProduct.MoveNext
  21.   Loop
  22.     rstProduct.MoveFirst
  23.     rstCustomer.MoveNext
  24. Loop
  25.  
  26. rstTransCustomer.Close
  27. rstProduct.Close
  28. rstCustomer.Close
  29. Set rstProduct = Nothing
  30. Set rstCustomer = Nothing
  31. Set rstTransCustomer = Nothing

9 2817
ADezii
8,834 Expert 8TB
Here is one Method you can use that will populate the TRANS_CUSTOMER TABLE with every combination of Customer and Product. I'm totally ignoring the fact that the Database needs some Normalization and Re-Structuring:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstCustomer As DAO.Recordset
  3. Dim rstProduct As DAO.Recordset
  4. Dim rstTransCustomer As DAO.Recordset
  5.  
  6. Set MyDB = CurrentDb
  7. Set rstCustomer = MyDB.OpenRecordset("CUSTOMER TABLE", dbOpenForwardOnly)
  8. Set rstProduct = MyDB.OpenRecordset("PRODUCT TABLE", dbOpenSnapshot)
  9. Set rstTransCustomer = MyDB.OpenRecordset("TRANS_CUSTOMER TABLE", dbOpenDynaset)
  10.  
  11. Do While Not rstCustomer.EOF
  12.   Do While Not rstProduct.EOF
  13.     With rstTransCustomer
  14.       .AddNew
  15.         ![Cust_id] = rstCustomer![Cust_id]
  16.         ![Cust_Name] = rstCustomer![Customers]
  17.         ![Product] = rstProduct![Product]
  18.       .Update
  19.     End With
  20.       rstProduct.MoveNext
  21.   Loop
  22.     rstProduct.MoveFirst
  23.     rstCustomer.MoveNext
  24. Loop
  25.  
  26. rstTransCustomer.Close
  27. rstProduct.Close
  28. rstCustomer.Close
  29. Set rstProduct = Nothing
  30. Set rstCustomer = Nothing
  31. Set rstTransCustomer = Nothing
Oct 20 '09 #2
NeoPa
32,556 Expert Mod 16PB
Are you looking for a way to add records into the [TRANS_CUSTOMER] table such that every customer has a record that matches with every product? A Cartesian Product in fact?
Oct 20 '09 #3
NeoPa
32,556 Expert Mod 16PB
If that's what you're looking for (both ADezii & I seem to read it that way), then this could hardly be easier as the two tables need simply be included without any type of JOIN in the query :
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [TRANS_CUSTOMER]
  2.             (Cust_ID,Customer,Prod_ID,Product)
  3. SELECT      tC.Cust_ID,
  4.             tC.Customer,
  5.             tP.Prod_ID,
  6.             tP.Product
  7. FROM        [CUSTOMER] AS tC,
  8.             [PRODUCT] AS tP
Oct 20 '09 #4
ADezii
8,834 Expert 8TB
NeoPa's solution is cleaner, more efficient, and right on point. I simply adjusted the SQL to conform with your Object Names and requirements (hope you didn't mind NeoPa). The SQL Statement has been tested and is fully operational.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [TRANS_CUSTOMER TABLE] ( Cust_ID, Cust_Name, Product )
  2. SELECT tC.Cust_ID, tC.Customers, tP.Product
  3. FROM [CUSTOMER TABLE] AS tC, [PRODUCT TABLE] AS tP;
Oct 21 '09 #5
NeoPa
32,556 Expert Mod 16PB
I think the three differences are down to confusion ADezii. The OP was not very well considered and I'm sure included some sloppy mistakes. Putting two & two together :
  1. The table names don't have the " TABLE" in them if I read the post correctly. It would be highly unlikely for a name to have an underline AND a space separating words.
  2. The OP forgot to include the Prod_ID value in his posted data. Clearly it makes little sense to have the description on its own, otherwise they would follow the same logic for the customer.
  3. The names of the fields too seem obviously to be transcribed sloppily. Why, for instance, use [Customers] to describe the customer name?
Oct 21 '09 #6
@ADezii


ADezii, Thanks so much man! It works beautifully. I will take care of Normalization later, thanks again. God bless!
Oct 21 '09 #7
ADezii
8,834 Expert 8TB
@JoeKid09
You are quite welcome, but you should use NeoPa's approach instead of mine.
Oct 21 '09 #8
Hey NeoPa thank so much for your help as well. I hope all is well, God Bless!
Nov 3 '09 #9
NeoPa
32,556 Expert Mod 16PB
All is well thank you Joe :)

I hope the same is true for you.
Nov 3 '09 #10

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

Similar topics

2
by: Fred | last post by:
Hi. How do I import while mapping an excel table to an access table please??? I've searched around and all I can find is a software product or code that does the same thing as the access...
2
by: JMCN | last post by:
hi i need some advice on whether if it would be better to use an append query or an update query. here is the situation, i have linked another database table to my current database. then i...
4
by: sheree | last post by:
I have 3 tables (amoung a few others) in a small access database. The tables are as follows: == AEReport -------- AEID (PK) RptCatelog GCRCID PatientID EvntDate
4
by: Shahzad | last post by:
dear respected gurus, I would like to knew how to apply append,insert query for a self table where no primary keys issues. i do have problem say there are 5 rows of single record, this is data...
1
by: girlkordic | last post by:
I have a linked table from an Oracle database (that contains HR information for my company)in an Access database. The Oracle DB is updated on a daily basis. I would like one of my tables in...
13
by: royaltiger | last post by:
I am trying to copy the inventory database in Building Access Applications by John L Viescas but when i try to run the database i get an error in the orders form when i click on the allocate...
8
by: Yitzak | last post by:
Hi is there a way to run a SQL Script to Update an Access Schema. Without using VBA code. E.g. in mssql server I can run script like if not exists (select * from syscolumns where name =...
2
by: Bubb | last post by:
I have an Access database with one table that I use for stuff I sell online. Each record has the following fields: Unique Id, Cost, and Item Description. I just obtained some more stuff with its...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...

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.