Solved

Run-time error 3201: You cannot add or change a record...

Posted on 2014-01-22
6
1,419 Views
Last Modified: 2014-01-22
I am receiving a runtime error when I try to run a piece of code. The first time I ran the code it worked well. Now everytime I click on the button the code is attached to, I get this message:

Run-time Error '3201':
You cannot add or change a record because a related record is required in table 'subtaskorders.'

Here is the code:

Private Sub cmdAdd_Click()

Dim strSql As String

strSql = "INSERT INTO Activities (ActTypeID, ActDesc, StoID, ToID) VALUES (" & Me.lsttype & ", '" & Me.txtactdesc & "', " & Me.cmbSTO & ", " & Me.cmbTO & ")"

CurrentDb.Execute strSql, dbFailOnError

End Sub

Open in new window


The part that is highlighted in the debugging is the line right before End Sub.

I have attached a copy of the database because it is almost always necessary.

Thank you, in advance, for the help!
PMAC-Weekly-Report-Database.accdb
0
Comment
Question by:Megin
6 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 400 total points
ID: 39800438
You're trying to add a record with the value of cmbST, but that combo's first column returns the value from SubTaskOrders.StoNo, which is not the correct value to store.

You'd have to change the query used for cmbST to include that field, and then refer specifically to that column in the combo. For example, change the SQL to include STOID in the first column, and the query should work.
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39800455
When you post a database with more than one object, you need to tell us what to look at.  So, I can't tell you exactly what is wrong but this message is actually clear.  Check the value of STOID in your insert statement.  It must be null or contain a valid value for an existing record in the 'subtaskorders' table
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 39800467
I got the attached copy working. You should really write some data validation code to check for valid values before the insert statement gets executed.
PMAC-Weekly-Report-Database.accdb
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Megin
ID: 39800478
aebea: Is the data validation code this part?:

error_handler:
    Debug.Print strSql
    Debug.Print "Err# " & Err.Number & ": " & Err.Description
    Resume exit_sub
0
 
LVL 10

Assisted Solution

by:Anthony Berenguel
Anthony Berenguel earned 100 total points
ID: 39800530
Megin, no. That is the error_handler section that will print to the immediate window whenever there is an error. In this event's case it will most likely be when a bad value is put into your strSql variable. For example, if you put type anything you want in that second combobox (the one for STOID) then a text value (instead of a number value) will be placed in the strSql variable for that field -- which will then cause the statement to fail when you go to execute it.

Data validation (for example) would be making sure all required fields are populated before the code tries to execute the sql command. Or making sure all data types are valid, etc.

Let me know if you have more questions about data validation.
0
 

Author Closing Comment

by:Megin
ID: 39800546
Thank you!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now