Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-22
6
Medium Priority
?
1,603 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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1600 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 40

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
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!

 

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 400 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

877 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