2
votes

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

2 Answers

3
votes

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.

0
votes

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