473,796 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populating one field in a table with one to many relation

Hi,

I have a main table named tblCrPurchase2 with following fields
CrPurchase2ID - key field
InvoiceDate
InvoiceNumber
PF1

I have a child table called tblCrPurchase2D etail (this is the many side
of the table) with following fields
CrPurchase2Deta ilID - Key field
CrPurchase2ID - for linking with tblCrPurchase2 table
ProdDesc
PurPr2
PurQty2
PF2

Figure 1
Data for tblCrPurchase2 is:
CrPurchase2ID InvoiceDate InvoiceNumber PF1
1 7/1/2005 1245 0.10
2 7/5/2005 32154 0.25

Figure 2
Data for tblCrPurchase2D etail is
CrPurchase2ID CrPurchase2Deta il ProdDesc PurPr2 PF2
1 1 Prod1 1.56 0
1 2 prod2 5.35 0
1 3 prod3 3.65 0
2 4 prod1 1.35 0
2 5 prod2 5.00 0
2 6 prod3 3.65 0

I want to update the first three records with PF2 = 0.10 and the last
three records with PF2 = 0.25 so end result would look like this
Figure 3
CrPurchase2ID CrPurchase2Deta il ProdDesc PurPr2 PF2
1 1 Prod1 1.56 0.10
1 2 prod2 5.35 0.10
1 3 prod3 3.65 0.10
2 4 prod1 1.35 0.25
2 5 prod2 5.00 0.25
2 6 prod3 3.65 0.25

By using a command button I put the following code on click event

Private Sub cmdMakechanges_ Click()
Dim db As Database
Dim rs As Recordset
Dim FirstPF As Double

Set db = CurrentDb

Set rs = db.OpenRecordse t("SELECT * FROM tblCrPurchase2 INNER JOIN
tblCrPurchase2D etail ON tblCrPurchase2. CrPurchase2ID =
tblCrPurchase2D etail.CrPurchas e2ID", dbOpenDynaset)

FirstPF = rs!PF1

rs.MoveFirst
Do While Not rs.EOF
rs.Edit
rs!PF2 = FirstPF
rs.Update
rs.MoveNext
Loop

rs.Close

End Sub
Figure 4
After running this code my query get populated as follows
CrPurchase2ID CrPurchase2Deta il ProdDesc PurPr2 PF2
1 1 Prod1 1.56 0.10
1 2 prod2 5.35 0.10
1 3 prod3 3.65 0.10
2 4 prod1 1.35 0.10
2 5 prod2 5.00 0.10
2 6 prod3 3.65 0.10

What I really want done is if I am at nth record of main table than the
code should pull up the data from many side of the table that relates
with one side of the table and populate PF2 with whatever is in PF1.
For example if I am at record 2 of main table with PF1 = 0.25 than it
should populate PF2 = 0.25 in many side of the table in only three
records. I.e record 4,5, and 6. because record 4,5, and 6 in many side
of table is related to record 2 on one side of table as the table shows

Figure 5
CrPurchase2ID CrPurchase2Deta il ProdDesc PurPr2 PF2
2 4 prod1 1.35 0.25
2 5 prod2 5.00 0.25
2 6 prod3 3.65 0.25
I hope I have made myself clear.

How do I code to get the results as shown?

Thanking you all in advance

Nov 13 '05 #1
2 1647
1) do you need to save a value that you can derive from existing data?
2) can you use an update query instead of code? Most of the times this
is more reliable and executes (way) faster

ha*******@yahoo .com wrote:

I want to update the first three records with PF2 = 0.10 and the last
three records with PF2 = 0.25 so end result would look like this
Figure 3
CrPurchase2ID CrPurchase2Deta il ProdDesc PurPr2 PF2
1 1 Prod1 1.56 0.10
1 2 prod2 5.35 0.10
1 3 prod3 3.65 0.10
2 4 prod1 1.35 0.25
2 5 prod2 5.00 0.25
2 6 prod3 3.65 0.25


--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html

Nov 13 '05 #2
Do you really need to do it in code? You could just run an update
query:

UPDATE tblCrPurchase2
SET ProdDesc = 0.10
WHERE CrPurchase2ID = 1

.... actually, you could dynamically create the query in code and
execute it there if you need to - probably easier (and faster running)
than messing with recordsets.

docmd.runsql strMyUpdateQuer y

Nov 13 '05 #3

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

Similar topics

1
2109
by: Svelte Poshy | last post by:
Populating a field in a table depending on another In my table customers i want to populate the field segmenid depending on the choice in the lookup field Typeid. Both fields, typeid and segmentid are placed in the table Customers. The field Typeid is a lookup field, looking in the table Type.
2
2299
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings has 55, then I want Rulings to have a number greater than 55. Two or more Hearings may be entered before a Ruling is entered. For example, Hearings with CaseIDs= 55, 56, and 57 may be entered before a Ruling or vice versa. There is no definite...
18
18422
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
3
6044
by: sck10 | last post by:
Hello, I am creating a form for users to enter information about a lab and the members of the lab. I have one form (FormView) that they use to enter information about that lab. The keyvalue is "LabLocation_ID". With an existing lab, they then need to add the members for that lab. So, what I am trying to do is the following. With the FormView of the Lab open, the user will click a button to open a FormView (InsertMode) and add a new...
117
18581
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of elements in the HTML to get everything just right. When you consider the class attribute on the DIV elements, there's not much size savings anymore for using DIV. There are other disadvantages to not using TABLE/TR/TD, such as the lack of ability...
39
3236
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
4
2663
by: Don Do | last post by:
Help I built a form/subform/subsubform setup using the access forms wizard. I have a table1 = parent, table2 = child, table3 = (grandchild?). There will be multiple records in table2 that tie to table1, and multiple records in table 3 that tie to table2. Both on "1 to many" joins. Each of the forms are bound to the fields in the respective tables. The subform & subsubform are datasheet view. When I enter any data, I
3
2841
by: CourtGuy | last post by:
Hi Folks, I've got a problem that's been confounding me for months. I work for a criminal court, and I'm trying to set up an Access database to track petitions filed by criminal defendants. I am using the court's case file number record as the primary key to make sure the database doesn't wind up with multiple entries for the same case number. However, a single case number can have other defendants charged in the same case number who...
4
5114
bugboy
by: bugboy | last post by:
I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'. I've been passed 2 variables from a form; $word and $def and i'm inserting them ok but i'm having trouble getting and passing on their ID's in order to insert them into the relation table 's' I've checked every query and they all work individually, so i...
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
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
10003
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
9050
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
7546
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...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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

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.