Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: 4 Weeks Ago
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!
best answer - posted 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
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,213
#2: 4 Weeks Ago

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


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
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#3: 4 Weeks Ago

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


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?
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#4: 4 Weeks Ago

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


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
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,213
#5: 4 Weeks Ago

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


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;
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#6: 4 Weeks Ago

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


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?
Newbie
 
Join Date: Oct 2009
Posts: 5
#7: 4 Weeks Ago

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


Quote:

Originally Posted by ADezii View Post

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



ADezii, Thanks so much man! It works beautifully. I will take care of Normalization later, thanks again. God bless!
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,213
#8: 4 Weeks Ago

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


Quote:

Originally Posted by JoeKid09 View Post

ADezii, Thanks so much man! It works beautifully. I will take care of Normalization later, thanks again. God bless!

You are quite welcome, but you should use NeoPa's approach instead of mine.
Newbie
 
Join Date: Oct 2009
Posts: 5
#9: 2 Weeks Ago

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


Hey NeoPa thank so much for your help as well. I hope all is well, God Bless!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#10: 2 Weeks Ago

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


All is well thank you Joe :)

I hope the same is true for you.
Reply