473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ACCESS: Form, 2 tables with constraint, fail to create new record

Hello, I have some doubts creating a new Access, Any help?
:) Thanks in advance.

I have 4 tables (* indicates pk)

Expand|Select|Wrap|Line Numbers
  1. 10Company
  2.   CompID *
  3.   CompName
  4.   CompDesc
  5.  
Expand|Select|Wrap|Line Numbers
  1. 20Section
  2.   SectCompID *
  3.   SectID *
  4.   SectName
  5.   SectDesc
  6.  
Expand|Select|Wrap|Line Numbers
  1. 30Jobs
  2.   JobCompID *
  3.   JobSectID *
  4.   JobID *
  5.   JobName
  6.   JobDesc
  7.  
Expand|Select|Wrap|Line Numbers
  1. 40Operations
  2.   OpeCompID *
  3.   OpeSectID * 
  4.   OpeJobID *
  5.   OpeOperID *
  6.   OpeName 
  7.   OpeDesc 
  8.  
All tables are 1-N dependant via the ID Field, this means I can not create a Section without a provided Company, I can not create a Job without a created Section and I can not Create an operation without a Job.

I know this is not nice E-R model, but Im given ER and requested to create the maintenance Form for the Operations.

Then I have created a form:
Expand|Select|Wrap|Line Numbers
  1. OperationsForm
  2.   ControlSource = 40Operations
  3.  
Then In the Form I have added the following fields:
Expand|Select|Wrap|Line Numbers
  1. OpeOperationTxt
  2.   ControlSource = OpeOperID
  3.  
  4. JobsComboBox 
  5.   ControlSource = OpeJobID
  6.   Row Source = Select JobID, JobSectID, JobCompID from 30Jobs
  7.   Bound Column =1
  8.   ColumnCount = 3
  9.   BeforeUpdate = CodeAfterColumns
  10.  
  11. OpeJobTxt 
  12.   ControlSource =  OpeJobID
  13.   enabled, locked = No
  14.  
  15. OpeSectionTxt 
  16.   ControlSource =  OpeSectID
  17.   enabled, locked = No
  18.  
  19. OpeCompanyTxt 
  20.   ControlSource =  OpeCompID
  21.   enabled, locked = No
  22.  

JobsComboBox.BeforeUpdate code
Expand|Select|Wrap|Line Numbers
  1.     Me.OpeJobTxt = Me.JobsComboBox.Column(0)
  2.     Me.OpeSectionTxt = Me.JobsComboBox.Column(1)
  3.     Me.OpeCompanyTxt = Me.JobsComboBox.Column(2)
  4.  
Then.... I have a few questions ...

1.- I type a value in the OpeOperationTxt, then I select a value from JobsComboBox combo, it updates the OpeJobTxt, OpeSectionTxt, OpeCompanyTxt correctly, then if I press tab, it should create a new record in the 40Operations table, but instead, I have an error:
"You cannot add or change a record because a related record is required in 30Jobs"

I dont understand why it asks this, as the record in 30Jobs table, exists (i selected it from the combobox).

2.- Second question is, how can i stop the form from creating new records, and to do it via clicking a button


Thanks a lot.

[UPDATE]: If i delete the 1-to-N relationship, it creates the row(record) in the 40Operations table
Feb 4 '16 #1

✓ answered by zmbd

+ Composit keys can be difficult to work with as everything has to be "just so;" however, I have played with them a few times.

+ Check your table indexes Access by default will insert indexes in to the table for any field ending (or often just containing) "ID", "key", "code", or "num" , this is set in the Ribbon/options/object designers/table design view/AutoIndex on Import/Create. Personally, I delete ALL of these
- Open the table in design view
- Ribbon>Design>Show/Hide>Indexes

Check for your primary key and delete any duplicated field indexes.... these duplicated indexes will cause no end of issues!

+ One of the other most common issues, is that what one thinks should be returned from a control isn't what is needed for the underlying table-record; thus, in your update code let's make sure what you think you are returning from the controls is what you should be returning:
Insert just before your Me.* = *:
Expand|Select|Wrap|Line Numbers
  1. With Me     
  2.      Debug.Print "OpeJobTxt = " & .JobsComboBox.Column(0)
  3.      Debug.Print "SectionTxt = " & .JobsComboBox.Column(1)
  4.      Debug.Print "OpeCompanyTxt = " & .JobsComboBox.Column(2)
  5. End With
Run your form, once done, <ctrl><G> should open the VBA editor and show the immediate window, therein should be our values.

How does the information output to the window match to the corresponding fields in the tables? Are numeric values being returned or string values? Are the Table Fields numeric or text and do these match?

+ If all looks "as it should" then I need a bit more information about your tables.

+ For each of your [*ID] fields (i.e. CompID, SectCompID, SectID) what is the data type, I am guessing Numeric(Long); however, I would rather not guess.
-- it wouldn't hurt to have the data type for all fields, we can add this to your original post if you like :-)

+ A more explicit detail about how your inter-table relationships are setup is needed...
Once again here I'm guessing:
(I know, It's customary to give these as 1:M; however, I'm doing this as I look at your tables...)
20Section
a1) [20Section]![SectCompID] M:1 [10Company]:[CompID]

30Jobs
b1) [30Jobs]![JobCompID] M:1 [10Company]:[CompID]
b2) [30Jobs]![JobSectID] M:1 [20Section]![SectID]

etc... this is what I currently envision your relationships as... (currently I have the fields set to numeric(long) in the primary key)


5 1216
Seth Schrock
2,965 Expert 2GB
Thank-you for providing such detail. It isn't often that a new user does that.

I believe that your problem is that you are using composite keys in non join tables. If you don't have natural single field primary keys, then just add an auto-number field. If you do this, then you can remove the OpeCompID, OpeSectID, and OpeOperID fields from the 40Operations table as this information is all related through your OpeJobID field. Otherwise, you are duplicating data which breaks normalization rules. See Database Normalization and Table Structures. If you need to see the information on the form about the CompID, SectID, and OperID, then build a query that has all the tables in it and then base your form on that. As soon as you select the JobID, the other fields will populate automatically without any code.
Feb 4 '16 #2
Hello,
Thanks for answering :)

I totally agree with you, about the model, but the database is not "mine" and i need to use it like it is designed :-(

Yes I have composite relation, and I have seen that after deleting it, the form is working.

So... the problem are the Relationships. But I can not delete them.

I don't understand why the validation is not working as the registry in 30Jobs exists.

Thanks
Feb 4 '16 #3
Seth Schrock
2,965 Expert 2GB
I'm not sure how to get around this without changing the tables. Sorry I can't help you more.
Feb 5 '16 #4
zmbd
5,501 Expert Mod 4TB
+ Composit keys can be difficult to work with as everything has to be "just so;" however, I have played with them a few times.

+ Check your table indexes Access by default will insert indexes in to the table for any field ending (or often just containing) "ID", "key", "code", or "num" , this is set in the Ribbon/options/object designers/table design view/AutoIndex on Import/Create. Personally, I delete ALL of these
- Open the table in design view
- Ribbon>Design>Show/Hide>Indexes

Check for your primary key and delete any duplicated field indexes.... these duplicated indexes will cause no end of issues!

+ One of the other most common issues, is that what one thinks should be returned from a control isn't what is needed for the underlying table-record; thus, in your update code let's make sure what you think you are returning from the controls is what you should be returning:
Insert just before your Me.* = *:
Expand|Select|Wrap|Line Numbers
  1. With Me     
  2.      Debug.Print "OpeJobTxt = " & .JobsComboBox.Column(0)
  3.      Debug.Print "SectionTxt = " & .JobsComboBox.Column(1)
  4.      Debug.Print "OpeCompanyTxt = " & .JobsComboBox.Column(2)
  5. End With
Run your form, once done, <ctrl><G> should open the VBA editor and show the immediate window, therein should be our values.

How does the information output to the window match to the corresponding fields in the tables? Are numeric values being returned or string values? Are the Table Fields numeric or text and do these match?

+ If all looks "as it should" then I need a bit more information about your tables.

+ For each of your [*ID] fields (i.e. CompID, SectCompID, SectID) what is the data type, I am guessing Numeric(Long); however, I would rather not guess.
-- it wouldn't hurt to have the data type for all fields, we can add this to your original post if you like :-)

+ A more explicit detail about how your inter-table relationships are setup is needed...
Once again here I'm guessing:
(I know, It's customary to give these as 1:M; however, I'm doing this as I look at your tables...)
20Section
a1) [20Section]![SectCompID] M:1 [10Company]:[CompID]

30Jobs
b1) [30Jobs]![JobCompID] M:1 [10Company]:[CompID]
b2) [30Jobs]![JobSectID] M:1 [20Section]![SectID]

etc... this is what I currently envision your relationships as... (currently I have the fields set to numeric(long) in the primary key)

Attached Images
File Type: jpg 965597_CompositKeyIssue.jpg (22.1 KB, 361 views)
Feb 5 '16 #5
Thanks a lot to you two.

Finally I got desperate, deleted all, cleaned the "Ribbon/options/object designers/table design view/AutoIndex on Import/Create." option, cleaned the indexes, and it worked fine.

Probably I had an error or an index giving me problems, so on recreating more carefully it worked.

thanks a lot again.
:)
Feb 8 '16 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Josh Strickland | last post by:
I am attempting to create an Access database which uses forms to enter data. The issue I am having is returning the query results from the Stored Procedure back in to the Access Form. ...
5
by: David Powell | last post by:
I have a database that was created by a member of my staff and he seems to have protected it so that only a form displays when the database is open. I need to ammend the database and add records...
14
by: alwayshouston | last post by:
Hi All! I am working on this very small database and I am confused in the designing a simple form. I only have three tables in the database. First Table: tblExpense Columns: ExpenseID ;...
2
by: paulwilliamsonremove | last post by:
Hi, How do I stop an Access (2003) form from it's default behaviour to automatically save a record when exiting the form? I want to use a save button instead. Thanks, -pw
6
by: erick-flores | last post by:
Hello all, Form A & Form B Form B opens when I click on a button from Form A. How do I setup Form B so it will always let me insert new records. Because right know, when I click on the button...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
5
by: wassimdaccache | last post by:
Dear expert; using access 2003 . I have a table name "tables" that contains "tableid" & "tabledesc" The idea is to design a form that contains for each record in "tables-->tableid" a...
3
by: bettyboo | last post by:
Hi I'm new to the forum and also a VERY new user of Access to develop databases. I'm building a DB for a driving instructor acquaintance, and he wants a button on the pupil data entry form which...
2
by: buddyr | last post by:
Hello, I saw an example of microsoft access form based on table. the form had combo box to select employee and and then labels and textboxes below. The form had a pie chart that changed to...
20
by: alekseiaiteno | last post by:
Hello all! Have researched the issue all day to to avail, therefore turning to your help for the solution. I have a Form A which is accessed via another Form B only (i.e. not possible for user...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
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,...
0
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...
0
jinu1996
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...

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.