Solved

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

Posted on 2014-01-22
6
1,452 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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.

910 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

24 Experts available now in Live!

Get 1:1 Help Now