Solved

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

Posted on 2014-01-22
6
1,517 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 37

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

734 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