By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,461 Members | 2,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,461 IT Pros & Developers. It's quick & easy.

Object Reference Error in Code

P: 1

I am trying to insert data from one table (Emp_Details) into another table (Requests) based on the employee ID entered in the access form. I have writen the following code:

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim rs As DAO.Recordset
  3. Dim rs1 As DAO.Recordset
  4. Dim sel As String
  5. 'Set con = New dao.Connection
  6. 'Set rs = New DAO.Recordset
  7. Set db = CurrentDb()
  8. Set rs = db.OpenRecordset("Emp_Details", dbOpenDynaset)
  9. Set rs1 = db.OpenRecordset("Requests", dbOpenDynaset)
  10. rs1.AddNew
  11. DoCmd.OpenQuery "Insert into Requests(ID, LName, FName, Manager, Email, Level) Select Emp_Details.ID, Emp_Details.LName, Emp_Details.FName, Emp_Details.EManager, Emp_Details.Email, Emp_Details.L from Emp_Details Where Emp_Details.ID = '" & txtID & "'"
  12. rs1.Update
  13. rs1.Close
  14. db.Close
  15. Set rs1 = Nothing
  16. Set db = Nothing
But it gives an error 7874 saying that MS Access cant fine the Object.

How can I solve this problem?
Thanks in advance,
Mar 21 '12 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 903
I think you had more of an error message than that but you truncated it. You also didn't indicated where the error occured.

But quickly looking at your code, your query references a control or variable called txtID - I don't see it as a variable and if it is a control it is not referenced properly.

Did you mean to use the variable sel instead of txtID?

Mar 21 '12 #2

Expert Mod 10K+
P: 12,355
Please use code tags when posting code.

The OpenQuery function is used to open an existing query object. Not to run SQL. To run SQL, use the RunSQL function.

Also, why did you create two recordsets? The only thing you're doing is adding a BLANK record. One of the recordsets you don't even use. The other one seems to want to do the same thing as the insert SQL but you never follow through.

All in all, the code is a mess.
Mar 21 '12 #3

Expert Mod 15k+
P: 31,398
Some of the points in this linked thread (Before Posting (VBA or SQL) Code) are very obvious, yet it seems you need them pointed out for you.

Your post suffers from various other avoidable problems if you'd simply read the threads explaining how to use the forums, or alternatively applied a minimal amount of consideration before submitting your question.

You have wasted a deal of time of those members trying to help you, so please be more considerate in future. A number of threads like this get deleted every day. You should try to ensure that none of your future questions follow that route.
Mar 22 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.