2

In a form, when pressing a button called "asignar", I want to insert values to an existing table called "movimientos"

Private Sub ASIGNAR_Click()
  Dim db As Database
  Dim rs As DAO.Recordset

  Set rs = db.OpenRecordset("MOVIMIENTOS")
  rs.AddNew
  rs("ESTATUSDOC").Value = "Blah"
  rs("FOLIOFED").Value = "Blah"
  rs("NOMBREDOC").Value = "Blah"
  rs("PREL").Value = "Blah"
  rs("CURP").Value = "Blah"
  rs.Update
End Sub

When I press run it keep showing error:

Error 91 object variable or with block variable not set

2 Answers 2

3

I think your syntax is a little off, your code should look like this:

Private Sub ASIGNAR_Click()

   Dim db As DAO.Database
   Dim rs As DAO.Recordset

   Set db = CurrentDb
   Set rs = db.OpenRecordset("MOVIMIENTOS", dbOpenDynaset, dbAppendOnly)

   rs.AddNew
   rs("ESTATUSDOC").Value = "Blah"
   rs("FOLIOFED").Value = "Blah"
   rs("NOMBREDOC").Value = "Blah"
   rs("PREL").Value = "Blah"
   rs("CURP").Value = "Blah"
   rs.Update

   rs.Close 
   Set rs = Nothing 
   Set db = Nothing

End Sub

The issue is primarily the way that you were declaring and setting your db variable. I have also adjusted the OpenRecordset to match what you are doing.

Sign up to request clarification or add additional context in comments.

2 Comments

All good, though don't forget to close opened recordsets rs.Close and clean your memory with Set rs = Nothing and Set db = Nothing
@MattHall Thanks for spotting that.
0

Changing

Set rs = db.OpenRecordset("MOVIMIENTOS") 

to

Set rs = CurrentDB.OpenRecordset("MOVIMIENTOS")

should fix it as well. You can then lose the Dim db As Database

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.