Home

VBA Code & Relationship setup to populate form fields on After Update event

%3CLINGO-SUB%20id%3D%22lingo-sub-1046652%22%20slang%3D%22en-US%22%3EVBA%20Code%20%26amp%3B%20Relationship%20setup%20to%20populate%20form%20fields%20on%20After%20Update%20event%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1046652%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20Thanks%20in%20advance%20for%20any%20help.%20This%20involves%20a%20little%20VBA%20code%20and%20three%20MS%20Access%20tables.%201)%20SR.%20Technology%20Officer%20(tblSTO)%2C%202)%20tblProjects%2C%203)%20tblDeliverables.%20Among%20other%20fields%2Cthe%20STO%20table%20has%20an%20autonumber%20STO_ID%20(primary%20index)%20and%20the%20STO_Name%20(text).%20The%20tblProject%20table%20has%20ProjectNo%20(text)%20and%20STO_ID.%20These%20two%20combined%20are%20unique%20and%20make%20the%20primary%20key.%20The%20tblDeliverables%20table%20has%20ProjectNo%2C%20STO_ID%2C%20ServiceName%20(text).%20These%20three%20combined%20are%20unique%20and%20make%20the%20primary%20key.%20I%20have%20a%20qryDeliverable%20query%20which%20includes%20all%20three%20tables%20and%20all%20the%20fields%20from%20each%20table.%20I%20have%20a%20form%20frmDeliverable%20which%20uses%20the%20qryDeliverables%20as%20the%20record%20source%20and%20has%20a%20combo%20box%20field%20to%20select%20the%20ProjectNo%20from%20the%20Project%20table.%20The%20ProjectNo%2C%20STOName%20(from%20STO%20table)%20are%20displayed%20in%20the%20combobox%20drop%20down.%20Once%20ProjectNo%20is%20selected%2C%20%3CSPAN%3E%26nbsp%3BI%20want%20the%20ProjectNo%2C%20STOName%2C%20and%20a%20couple%20other%20fields%20from%20the%20qryDeliverables%20displayed%2C%20and%20a%20field%20(DesignerPlanner)%20I%20try%20to%20populate%20using%20some%20VBA%20in%20an%20%22After%20Update%22%20event.%3C%2FSPAN%3E.%20Depending%20on%20how%20I%20play%20with%20the%20relationships%20and%20the%20VBA%20code%2C%20it%20either%20doesn't%20populate%20the%20fields%2C%20or%20the%20add%20record%20controls%20for%20the%20form%20disappear%2C%20or%20I%20get%20some%20relationship%20error.%3C%2FP%3E%3CP%3EA%20little%20history....I%20had%20this%20working%20perfectly%20when%20using%20an%20autonumber%20ID%20field%20(primary%20index)%20in%20both%20the%20Project%20and%20Deliverable%20tables.%20However%2C%20a%20new%20requirement%20came%20in%20to%20be%20able%20to%20be%20able%20to%20pre-populate%20(one%20time)%20the%20Deliverables%20table%20via%20import%20from%20a%20spreadsheet%20of%20records.%20With%20the%20Project%20file%20already%20populated%2C%20the%20spreadsheet%20to%20be%20imported%20into%20the%20Deliverables%20table%20has%20unique%20project%20numbers%20(ProjectNo)%20and%20a%20common%20STO_ID%20across%20all%20the%20records%20(just%20as%20a%20starting%20point)%2C%20but%20can't%20possibly%20have%20each%20project%20ID%20number%2C%20so%20the%20index%20requirement%20wasn't%20met.%20I%20had%20to%20delete%20the%20project%20ID%20field%20from%20the%20Deliverables%20table%20and%20use%20the%20ProjectNo%20and%20STO_ID%20combined%20as%20the%20primary%20index.%20This%20was%20the%20start%20of%20all%20my%20problems.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20VBA%20code%20(SQL%20Statements)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Project_No_AfterUpdate()%3CBR%20%2F%3EDim%20rs%20As%20DAO.Recordset%3CBR%20%2F%3EDim%20qdf%20As%20DAO.QueryDef%3CBR%20%2F%3EDim%20strSQL%20As%20String%3CBR%20%2F%3EIf%20Not%20IsNull(ProjectNo)%20Then%3CBR%20%2F%3ESet%20rs%20%3D%20CurrentDb.OpenRecordset(%22qryProjects%22)%3CBR%20%2F%3EMe.STO_ID%20%3D%20rs!STO_ID%3CBR%20%2F%3EMe.DesignerPlanner%20%3D%20%22Chiu%2C%20Derek%3B%22%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20this%20is%20clear%20and%20any%20help%20is%20appreciated.%20Thanks%20so%20much!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1046652%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1052142%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Code%20%26amp%3B%20Relationship%20setup%20to%20populate%20form%20fields%20on%20After%20Update%20event%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1052142%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F477512%22%20target%3D%22_blank%22%3E%40hfreedman1957%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECompound%20PKs%20are%20never%20a%20good%20idea.%20You%20should%20have%20a%20PK%20on%20each%20table%20that%20is%20a%20single%2C%20Autonumber%20column.%20And%20it%20should%20be%20meaningless%20to%20the%20user.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20that%20doesn't%20mean%20that%20you%20can't%20have%20a%20composite%20unique%20index%20as%20well%20that%20can%20be%20used%20to%20enforce%20constraints.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EManaging%20relationships%20with%20compound%20PKs%20is%20near%20impossible.%20So%20let's%20see%20if%20we%20can%20simplify%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EtblSTO%20has%20a%20good%20PK%20-%20STO_ID%20but%20tblProject%20should%20have%20an%20Autonumber%20PK%2C%20let's%20call%20it%20Project_ID.%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20can%20also%20have%20the%20natural%20key%20'ProjectNo'%20which%20should%20not%20be%20the%20PK%20but%20it%20can%20have%20a%20unique%20index%26nbsp%3B%20to%20prevent%20two%20instances%20of%20the%20same%20ProjectNo.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EtblProject%20already%20has%20a%20column%20STO_ID%20-%20this%20is%20the%20FK%20that%20relates%20to%20tblSTO.STO_ID.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20this%20a%20One%20(STO)%20to%20Many%20Projects%3F%20or%20are%20you%20trying%20to%20create%20a%20Many%20To%20Many%20relationship%3F%20ie%20can%20one%20project%20have%20more%20than%20one%20STO%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20it's%20the%20latter%2C%20then%20you%20would%20be%20better%20off%20with%203%20tables%20not%202%20-%20tblSTO%2C%20tblProject%20and%20tblSTOProject.%20Each%20with%20a%20single%20column%20PK.%3C%2FP%3E%3CP%3EtblSTOProject%20would%20then%20have%20STOProject_ID%20as%20its%20PK%20and%20two%20FK%20columns%20-%20STO_ID%20and%20Project_ID.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20then%20put%20a%20composite%20unique%20index%20on%20these%20two%20FK%20columns%20in%20tblSTOProject%20to%20apply%20the%20constraint%20of%20having%20only%20one%20instance%20of%20a%20STO%20for%20a%20Project.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20then%20use%20the%20autonumber%20PK%20of%20tblSTOProject%20to%20join%20to%20tblDeliverable.STOProject_ID%20knowing%20that%20the%20unique%20composite%20index%20ensures%20that%20STO%20combined%20with%20Project%20is%20unique.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20you%20could%20put%20another%20composite%20unique%20index%20on%20tblDeliverable%20including%20STOProject_ID%20and%20Service_Name%20to%20ensure%20you%20can%20only%20have%20one%20instance%20of%20a%20Service%20for%20each%20combination%20or%20STO%20and%20Project.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20flick%20the%20Compound%20PKs%2C%20use%20single%20column%20Autonumber%20PKs%20and%20use%20composite%20unique%20indexes%20to%20enforce%20the%20business%20rules.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20attached%20ACCDB%20-%20check%20out%20the%20Relationship%20Diagram%2C%20Indexes%20on%20each%20table%2C%20the%20query%20and%20the%20form.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1053588%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Code%20%26amp%3B%20Relationship%20setup%20to%20populate%20form%20fields%20on%20After%20Update%20event%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1053588%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F477512%22%20target%3D%22_blank%22%3E%40hfreedman1957%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewhen%20you%20look%20at%20the%20indexes%20-%20you%20will%20at%20first%20see%20that%20I%20haven't%20set%20the%20composite%20indexes%20to%20be%20unique.%20So%20currently%20you%20can%20create%20duplicate%20records.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20that%20first%2C%20then%20delete%20the%20duplicates%2C%20set%20the%20Indexes%20to%20be%20unique%20and%20then%20try%20to%20create%20duplicates.%20You%20shouldn't%20be%20able%20to.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20the%20main%20points%20are%20-%3C%2FP%3E%3CP%3EGet%20your%20table%20structures%20and%20relationships%20right%3C%2FP%3E%3CP%3EUse%20indexes%2C%20not%20compound%20PKs%2C%20to%20enforce%20business%20rules%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1056747%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Code%20%26amp%3B%20Relationship%20setup%20to%20populate%20form%20fields%20on%20After%20Update%20event%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1056747%22%20slang%3D%22en-US%22%3EThanks%20so%20much%20for%20the%20response.%20Originally%2C%20I%20had%20an%20autonumber%20ID%20field%20for%20each%20of%20the%20three%20tables%20and%20everything%20worked%20great.%20They%20then%20wanted%20to%20import%20the%20records%20into%20the%20deliverables%20table%2C%20but%20had%20no%20way%20to%20know%20the%20project%20ID%20value%20which%20is%20why%20we%20removed%20it%20and%20used%20the%20project%20number%20as%20the%20key.%20However%2C%20incorporating%20both%20is%20worth%20a%20shot.%20I'll%20play%20with%20that%20and%20report%20back.%20Unfortunately%2C%20I%20won't%20be%20back%20at%20work%20for%20over%20a%20month%20so%20this%20will%20have%20to%20wait.%20Thanks%20again!%3C%2FLINGO-BODY%3E
hfreedman1957
New Contributor

Hi, Thanks in advance for any help. This involves a little VBA code and three MS Access tables. 1) SR. Technology Officer (tblSTO), 2) tblProjects, 3) tblDeliverables. Among other fields,the STO table has an autonumber STO_ID (primary index) and the STO_Name (text). The tblProject table has ProjectNo (text) and STO_ID. These two combined are unique and make the primary key. The tblDeliverables table has ProjectNo, STO_ID, ServiceName (text). These three combined are unique and make the primary key. I have a qryDeliverable query which includes all three tables and all the fields from each table. I have a form frmDeliverable which uses the qryDeliverables as the record source and has a combo box field to select the ProjectNo from the Project table. The ProjectNo, STOName (from STO table) are displayed in the combobox drop down. Once ProjectNo is selected,  I want the ProjectNo, STOName, and a couple other fields from the qryDeliverables displayed, and a field (DesignerPlanner) I try to populate using some VBA in an "After Update" event.. Depending on how I play with the relationships and the VBA code, it either doesn't populate the fields, or the add record controls for the form disappear, or I get some relationship error.

A little history....I had this working perfectly when using an autonumber ID field (primary index) in both the Project and Deliverable tables. However, a new requirement came in to be able to be able to pre-populate (one time) the Deliverables table via import from a spreadsheet of records. With the Project file already populated, the spreadsheet to be imported into the Deliverables table has unique project numbers (ProjectNo) and a common STO_ID across all the records (just as a starting point), but can't possibly have each project ID number, so the index requirement wasn't met. I had to delete the project ID field from the Deliverables table and use the ProjectNo and STO_ID combined as the primary index. This was the start of all my problems.

 

Here is the VBA code (SQL Statements)

 

Private Sub Project_No_AfterUpdate()
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
If Not IsNull(ProjectNo) Then
Set rs = CurrentDb.OpenRecordset("qryProjects")
Me.STO_ID = rs!STO_ID
Me.DesignerPlanner = "Chiu, Derek;"
End If
End Sub

 

I hope this is clear and any help is appreciated. Thanks so much!

3 Replies

@hfreedman1957 

Compound PKs are never a good idea. You should have a PK on each table that is a single, Autonumber column. And it should be meaningless to the user.

 

But that doesn't mean that you can't have a composite unique index as well that can be used to enforce constraints.

 

Managing relationships with compound PKs is near impossible. So let's see if we can simplify this.

 

tblSTO has a good PK - STO_ID but tblProject should have an Autonumber PK, let's call it Project_ID. 

It can also have the natural key 'ProjectNo' which should not be the PK but it can have a unique index  to prevent two instances of the same ProjectNo.

 

tblProject already has a column STO_ID - this is the FK that relates to tblSTO.STO_ID.

 

Is this a One (STO) to Many Projects? or are you trying to create a Many To Many relationship? ie can one project have more than one STO?

 

If it's the latter, then you would be better off with 3 tables not 2 - tblSTO, tblProject and tblSTOProject. Each with a single column PK.

tblSTOProject would then have STOProject_ID as its PK and two FK columns - STO_ID and Project_ID.

 

You can then put a composite unique index on these two FK columns in tblSTOProject to apply the constraint of having only one instance of a STO for a Project.

 

You can then use the autonumber PK of tblSTOProject to join to tblDeliverable.STOProject_ID knowing that the unique composite index ensures that STO combined with Project is unique.

 

Then you could put another composite unique index on tblDeliverable including STOProject_ID and Service_Name to ensure you can only have one instance of a Service for each combination or STO and Project.

 

So flick the Compound PKs, use single column Autonumber PKs and use composite unique indexes to enforce the business rules.

 

In the attached ACCDB - check out the Relationship Diagram, Indexes on each table, the query and the form.

 

@hfreedman1957 

when you look at the indexes - you will at first see that I haven't set the composite indexes to be unique. So currently you can create duplicate records.

 

Try that first, then delete the duplicates, set the Indexes to be unique and then try to create duplicates. You shouldn't be able to.

 

So the main points are -

Get your table structures and relationships right

Use indexes, not compound PKs, to enforce business rules

Thanks so much for the response. Originally, I had an autonumber ID field for each of the three tables and everything worked great. They then wanted to import the records into the deliverables table, but had no way to know the project ID value which is why we removed it and used the project number as the key. However, incorporating both is worth a shot. I'll play with that and report back. Unfortunately, I won't be back at work for over a month so this will have to wait. Thanks again!
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
Extentions Synchronization
Deleted in Discussions on
3 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies