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!
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: - Dim MyDB As DAO.Database
-
Dim rstCustomer As DAO.Recordset
-
Dim rstProduct As DAO.Recordset
-
Dim rstTransCustomer As DAO.Recordset
-
-
Set MyDB = CurrentDb
-
Set rstCustomer = MyDB.OpenRecordset("CUSTOMER TABLE", dbOpenForwardOnly)
-
Set rstProduct = MyDB.OpenRecordset("PRODUCT TABLE", dbOpenSnapshot)
-
Set rstTransCustomer = MyDB.OpenRecordset("TRANS_CUSTOMER TABLE", dbOpenDynaset)
-
-
Do While Not rstCustomer.EOF
-
Do While Not rstProduct.EOF
-
With rstTransCustomer
-
.AddNew
-
![Cust_id] = rstCustomer![Cust_id]
-
![Cust_Name] = rstCustomer![Customers]
-
![Product] = rstProduct![Product]
-
.Update
-
End With
-
rstProduct.MoveNext
-
Loop
-
rstProduct.MoveFirst
-
rstCustomer.MoveNext
-
Loop
-
-
rstTransCustomer.Close
-
rstProduct.Close
-
rstCustomer.Close
-
Set rstProduct = Nothing
-
Set rstCustomer = Nothing
-
Set rstTransCustomer = Nothing
9 2817
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: - Dim MyDB As DAO.Database
-
Dim rstCustomer As DAO.Recordset
-
Dim rstProduct As DAO.Recordset
-
Dim rstTransCustomer As DAO.Recordset
-
-
Set MyDB = CurrentDb
-
Set rstCustomer = MyDB.OpenRecordset("CUSTOMER TABLE", dbOpenForwardOnly)
-
Set rstProduct = MyDB.OpenRecordset("PRODUCT TABLE", dbOpenSnapshot)
-
Set rstTransCustomer = MyDB.OpenRecordset("TRANS_CUSTOMER TABLE", dbOpenDynaset)
-
-
Do While Not rstCustomer.EOF
-
Do While Not rstProduct.EOF
-
With rstTransCustomer
-
.AddNew
-
![Cust_id] = rstCustomer![Cust_id]
-
![Cust_Name] = rstCustomer![Customers]
-
![Product] = rstProduct![Product]
-
.Update
-
End With
-
rstProduct.MoveNext
-
Loop
-
rstProduct.MoveFirst
-
rstCustomer.MoveNext
-
Loop
-
-
rstTransCustomer.Close
-
rstProduct.Close
-
rstCustomer.Close
-
Set rstProduct = Nothing
-
Set rstCustomer = Nothing
-
Set rstTransCustomer = Nothing
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?
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 : - INSERT INTO [TRANS_CUSTOMER]
-
(Cust_ID,Customer,Prod_ID,Product)
-
SELECT tC.Cust_ID,
-
tC.Customer,
-
tP.Prod_ID,
-
tP.Product
-
FROM [CUSTOMER] AS tC,
-
[PRODUCT] AS tP
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. - INSERT INTO [TRANS_CUSTOMER TABLE] ( Cust_ID, Cust_Name, Product )
-
SELECT tC.Cust_ID, tC.Customers, tP.Product
-
FROM [CUSTOMER TABLE] AS tC, [PRODUCT TABLE] AS tP;
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 : - 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.
- 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.
- The names of the fields too seem obviously to be transcribed sloppily. Why, for instance, use [Customers] to describe the customer name?
@ADezii
ADezii, Thanks so much man! It works beautifully. I will take care of Normalization later, thanks again. God bless!
@JoeKid09
You are quite welcome, but you should use NeoPa's approach instead of mine.
Hey NeoPa thank so much for your help as well. I hope all is well, God Bless!
NeoPa 32,556
Expert Mod 16PB
All is well thank you Joe :)
I hope the same is true for you.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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....
|
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,...
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |