98.7% Of all customers recommend us, we're so confident about our results we publish all reviews and stats
View Live Stats View ReviewsForum home » Delegate support and help forum » Microsoft Access Training and help » Creating a new table record from a form
Creating a new table record from a form
Resolved · Medium Priority · Version 2002/XP
Joe has attended:
Access Intermediate course
Creating a new table record from a form
Dear BestSTL,
(Please see accompanying email for any diagrams)
I have a table (TBL_STORES) with a list of stores, each identified with an autonumber primary key (ID_Store). Because many (but fewer than half) have a company name as well, I've created a separate table (TBL_STORES_CoName) to hold these company names - see diagram.
I've created a data entry form for stores including both these tables, amongst others. The form has a TextBox (TextBox_CoName), showing a company name if a store has one, blank when there isn't one.
My problem is when I want to enter a new company name, I also need ID_Store to be written to TBL_STORES_CoName, so when I return to the form, there is a link with the store (and the company name will show).
At present, if I enter a company name in the form, it is written to TBL_STORES_CoName, but there is no link with the store.
In other words, I need a way to write to two fields at the same time in the new record of TBL_STORES_CoName.
I tried with VBA:
Private Sub TextBox_CoName_LostFocus()
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO TBL_STORES_CoName (Store_ID, CoName) SELECT [TextBox_ID_Store] AS Store_ID, [TextBox_CoName] AS CoName;"
DoCmd.SetWarnings True
End Sub
When I go to the next record, or try to close the form, I get the following message:
"Index or primary key cannot contain a Null value" and I can't forward through the form records.
However, when I look at TBL_STORES_CoName, the new record has been entered. When returning to the form, the new entry also appears correctly.
I need to get rid of this error message so I can go to the next record in the form.
Alternatively if you have a better solution (with or without VBA) I'd be happy to try it.
Best regards
Joe
RE: Creating a new table record from a form
Hi Joe,
Thank you for the forum question.
You do not need any VBA to do this. Your problem is that you have a wrong relationship between the table. You will need to change the relationship to a one to one relationship. This is the only way Access will recognise what you want.
Please find attached example.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Attached files...
RE: Creating a new table record from a form
Hi Joe
Sorry I sent you a 2010 version. Please look at this one attached here.
I have also created a form where you can add the information in the two tables.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Attached files...
RE: Creating a new table record from a form
Dear Jens
Many thanks for your example.
Before looking at any forms of mine, I tried to apply your table properties to my database but run into a problem regarding Enforcing Referential Integrity.
First off, in case it makes any difference, I just wanted to say the two tables TBL_STORES & TBL_STORES_CoName were not created individually then linked. TBL_STORES included the field CoName. TBL_STORES was duplicated (renamed TBL_STORES_CoName) and all duplicate fields, records deleted from the appropriate tables, THEN a relationship was made by me. Access made it One-To-Many, so I left it.
To make my tables the same as your example I…
- Deleted the existing relationship
- Checked TBL_STORES field ID_Store was an auto number & Indexed (No Duplicates) like yours
- Altered TBL_STORES_CoName field Store_ID to be a primary key & Indexed (No Duplicates) like yours
- Tried to re-make the relationship…
The ‘Edit Relationships’ dialog now shows One-To-One but I can’t Enforce Referential Integrity.
- I’ve checked for duplicates in TBL_STORES_CoName with a query, but there are none.
- Referential Integrity was enforced with my previous One-To-Many relationship and I haven’t edited any records, so I don’t understand what the problem is.
Do you know why I can’t Enforce Referential Integrity?
Best regards.
Joe
RE: Creating a new table record from a form
Hi Joe,
Two things can stop Access from excepting Enforce Referential Integrity. Wrong datatype. Make sure that you have Autonumber in the "Parent" table as primary key and Number in the "Child" table as primary key. Make sure that you in the "child" table doesn't have any values you you do not have in the "parent" table.
I hope this will fix the issue.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
RE: Creating a new table record from a form
Dear Jens,
Access seems to be having a bad time!
After checking again on my 'real' data base without change, I created another blank one with two tables, one with a look-up so there is no chance I take a wrong ID number from the related table.
- Both have primary keys set to ‘Long Integer’
- All other attributes are the same as far as I can see
When I try to create the relationship with ‘Enforcing Referential Integrity’ in the relationships window I get the error:
“Data in the table ‘Stores’ violates Referential Integrity rules.
For example, there may be records relating to an employee in the related table, but no record for the employee in the primary table.”
I can’t for the life of me see what’s wrong.
I’ve attached the database to an email to info@stl-training.co.uk
Best regards.
RE: Creating a new table record from a form
Hi Joe,
Have a look at the attached file. I have changed your primary keys and created the relationship. I have also created a form which will work as you want.
To create a one to one relationship the relationship must go from one primary key to another primary key with the same data type (well if one is an autonumber the other one must be a number).
I have also created another option if you want your autonumber to be a primary key (see the tables new company and new store an also the form to enter the records in the new tables).
I hope this will solve your issue.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector
Attached files...
RE: Creating a new table record from a form
Dear Jens,
Thank you for your answer.
I looked at the example where you joined my tables with a one-to-one relationship, and first thought the join was between two fields of dissimilar data types, but see now the field ‘store’ was used instead of ‘ID_Store’ as the primary key (in table ‘Stores’).
Because I didn’t see this at first, I went back to my original example to recreate the relationship. Against all that I’ve read about creating table relationships, it DOES seem to matter from which table you drag the relationship line (at least for a one-to-one for the Access version I have). In my example creating a line FROM Companies/Store_ID TO Stores/ID_Store doesn’t let you choose Referential Integrity. Dragging the line the other way round does!
I’ve tried this on my main database and it works there too.
Thank you for all your help on this question.
Joe
Training information:
See also:
Welcome. Please choose your application (eg. Excel) and then post your question. Our Microsoft Qualified trainers will then respond within 24 hours (working days). Frequently Asked Questions
Any suggestions, questions or comments? Please post in the Improve the forum thread. |
Access tip:Duplicating an EntryTo duplicate the entry press CTRL+' (apostrophe)this will copy the contents of the previous entry in the same field. |