Solved Copy Data code (1 Viewer)

rondon

Member
Local time
Tomorrow, 07:40
Joined
May 16, 2020
Messages
53
I posted this in Forms but think I need a code solution- I have 3 tables all linked by a Report_ID. I would like to populate a field from the other form frmRecommendations if a value is entered into a field Agencies_ID that matches a recommendation number in frmRecommendations . I have attached a screen shot of the relationship and forms it is a lot clearer there. As usual any help appreciated. I also understand that I have most like approached this with the wrong structure but if code can solve the problem that would be great.
 

Attachments

  • Database overview.zip
    149 KB · Views: 115

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:40
Joined
Feb 19, 2002
Messages
43,352
1. Tables store data not forms. Forms are the window into what's in the tables. They also provide the means to add/change/delete data as well as to view it.
2. Do NOT duplicate data in other tables. Agency does not belong in tbl_Reports_tabled. That creates a pathological connection. How can you have many agencies related to a report but store a single Agency in the report record.
3. If you haven't gotten very far in your development (and even if you think it is too much work), I recomend that you change your column and other object names to follow best practices naming standards. NO spaces, NO special characters. ONLY A-Z, a-z, 1-9, and the underscore "_".
4. Your code should be generating the RecNum. Do not leave this to the user. When a new record is added to tbl_Recommendations, use:
Code:
If IsNull(Me.RecNum) Then
    RecNum = Nz(DMax("RecNum", "tbl_Recommendation", "Report_ID = " & Me.Report_ID), 0) +1  In the form's BeforeUpdate event.
End If
5. Finally, we're getting to your question. If Agencies are conneted to Recommendations, then your relationship is incorrect. Remove Report_ID from tbl_Agencies_ID (a better name might be tbl_AgencyAssigned and change the PK name accordingly) and replace it with Rec_ID. Use a combo on the Recommendations subform to pick the agency to assign. If multiple agencies can be assigned to the same recommendation, then you have a many-many relationship and you need a fourth table (usually called a Junction table) between tbl_Recommendations and tbl_Agencies_ID. The junction table typically contains just two fields. The two FK's of the tables being related.
 

rondon

Member
Local time
Tomorrow, 07:40
Joined
May 16, 2020
Messages
53
Thank you for the comprehensive response. I will take the time and address the points 1-4 and fix up all of the naming conventions and add the code. I will restructure my tables as per 5 and see if that helps me understand it more. I will get back to you once I have completed the above. Again thanks Ron
 

rondon

Member
Local time
Tomorrow, 07:40
Joined
May 16, 2020
Messages
53
1. Tables store data not forms. Forms are the window into what's in the tables. They also provide the means to add/change/delete data as well as to view it.
2. Do NOT duplicate data in other tables. Agency does not belong in tbl_Reports_tabled. That creates a pathological connection. How can you have many agencies related to a report but store a single Agency in the report record.
3. If you haven't gotten very far in your development (and even if you think it is too much work), I recomend that you change your column and other object names to follow best practices naming standards. NO spaces, NO special characters. ONLY A-Z, a-z, 1-9, and the underscore "_".
4. Your code should be generating the RecNum. Do not leave this to the user. When a new record is added to tbl_Recommendations, use:
Code:
If IsNull(Me.RecNum) Then
    RecNum = Nz(DMax("RecNum", "tbl_Recommendation", "Report_ID = " & Me.Report_ID), 0) +1  In the form's BeforeUpdate event.
End If
5. Finally, we're getting to your question. If Agencies are conneted to Recommendations, then your relationship is incorrect. Remove Report_ID from tbl_Agencies_ID (a better name might be tbl_AgencyAssigned and change the PK name accordingly) and replace it with Rec_ID. Use a combo on the Recommendations subform to pick the agency to assign. If multiple agencies can be assigned to the same recommendation, then you have a many-many relationship and you need a fourth table (usually called a Junction table) between tbl_Recommendations and tbl_Agencies_ID. The junction table typically contains just two fields. The two FK's of the tables being related.
1. Tables store data not forms. Forms are the window into what's in the tables. They also provide the means to add/change/delete data as well as to view it.
2. Do NOT duplicate data in other tables. Agency does not belong in tbl_Reports_tabled. That creates a pathological connection. How can you have many agencies related to a report but store a single Agency in the report record.
3. If you haven't gotten very far in your development (and even if you think it is too much work), I recomend that you change your column and other object names to follow best practices naming standards. NO spaces, NO special characters. ONLY A-Z, a-z, 1-9, and the underscore "_".
4. Your code should be generating the RecNum. Do not leave this to the user. When a new record is added to tbl_Recommendations, use:
Code:
If IsNull(Me.RecNum) Then
    RecNum = Nz(DMax("RecNum", "tbl_Recommendation", "Report_ID = " & Me.Report_ID), 0) +1  In the form's BeforeUpdate event.
End If
5. Finally, we're getting to your question. If Agencies are conneted to Recommendations, then your relationship is incorrect. Remove Report_ID from tbl_Agencies_ID (a better name might be tbl_AgencyAssigned and change the PK name accordingly) and replace it with Rec_ID. Use a combo on the Recommendations subform to pick the agency to assign. If multiple agencies can be assigned to the same recommendation, then you have a many-many relationship and you need a fourth table (usually called a Junction table) between tbl_Recommendations and tbl_Agencies_ID. The junction table typically contains just two fields. The two FK's of the tables being related.

Thanks, yes I have created new tables including a junction table and it appears to be working. Although I have spent some time with the old DB, I am going to start from new using the new structure.
 

rondon

Member
Local time
Tomorrow, 07:40
Joined
May 16, 2020
Messages
53

Micron

AWF VIP
Local time
Today, 17:40
Joined
Oct 20, 2018
Messages
3,478
Thanks for this I am starting from new
Then as long as you're starting over, I suggest you look at some other basics. If just one of these prevents you from falling into a common trap it will be time well spent (some of the articles seem sparse, but there are links within them that are worth following):

Normalization Parts I, II, III, IV, and V
and/or

Entity-Relationship Diagramming: Part I, II, III and IV

How do I Create an Application in Microsoft Access?

The evils of lookup fields - http://access.mvps.org/access/lookupfields.htm
Table and PK design tips - http://www.fmsinc.com/free/newtips/primarykey.asp
About calculated table fields - http://allenbrowne.com/casu-14.html
About Multi Value Fields - http://www.mendipdatasystems.co.uk/multivalued-fields/4594468763
 

rondon

Member
Local time
Tomorrow, 07:40
Joined
May 16, 2020
Messages
53
1. Tables store data not forms. Forms are the window into what's in the tables. They also provide the means to add/change/delete data as well as to view it.
2. Do NOT duplicate data in other tables. Agency does not belong in tbl_Reports_tabled. That creates a pathological connection. How can you have many agencies related to a report but store a single Agency in the report record.
3. If you haven't gotten very far in your development (and even if you think it is too much work), I recomend that you change your column and other object names to follow best practices naming standards. NO spaces, NO special characters. ONLY A-Z, a-z, 1-9, and the underscore "_".
4. Your code should be generating the RecNum. Do not leave this to the user. When a new record is added to tbl_Recommendations, use:
Code:
If IsNull(Me.RecNum) Then
    RecNum = Nz(DMax("RecNum", "tbl_Recommendation", "Report_ID = " & Me.Report_ID), 0) +1  In the form's BeforeUpdate event.
End If
5. Finally, we're getting to your question. If Agencies are conneted to Recommendations, then your relationship is incorrect. Remove Report_ID from tbl_Agencies_ID (a better name might be tbl_AgencyAssigned and change the PK name accordingly) and replace it with Rec_ID. Use a combo on the Recommendations subform to pick the agency to assign. If multiple agencies can be assigned to the same recommendation, then you have a many-many relationship and you need a fourth table (usually called a Junction table) between tbl_Recommendations and tbl_Agencies_ID. The junction table typically contains just two fields. The two FK's of the tables being related.

I have enter the code but the RecNum is set as a number field and has a zero when a new record is created is this preventing the code from working

Private Sub RecNum_BeforeUpdate(Cancel As Integer)
If IsNull(Me.RecNum) Then
RecNum = Nz(DMax("RecNum", "tblRecommendation", "ReportID = " & Me.ReportID), 0) + 1
End If
End Sub
 

Users who are viewing this thread

Top Bottom