Solved

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

Posted on 2014-01-22
6
1,487 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 36

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

820 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