I’m not an expert in Access and/or programming; so, it’s pretty sure that my question would be sort of dumb.. yet, I need your help, so.. I dared to come and ask 🙂
So, here it is the thing: I’ve a form which, when closed, will insert and update some records at a previously created table via VBA code. The thing is that I’ve created the code as close as I know for doing sucha task, but I keep recieving the error message ’91’ from the VBA compiler: “Object variable or with block variable not set”.
Could anyone help me with this issue? Thanks a lot in advanced; the full code I’m using is as follows:
Private Sub Form_Close()
Dim dbs As Database
Dim rst As recordSet
Dim auxGastoId, auxFecha
Dim auxReg
DoCmd.Echo False
DoCmd.SetWarnings False
DoCmd.OpenQuery "SueldosNuevoRegistroConsulta"
DoCmd.SetWarnings True
DoCmd.Echo True
auxGastoId = DMax("id_gasto", "Gastos")
auxFecha = DLookup("fecha_gasto", "Gastos", "id_gasto = " & auxGastoId)
Set db = CurrentDb()
Set rst = db.OpenRecordset("Empleados", dbOpenDynaset)
rst.MoveFirst
Do While rst.EOF = False
If (Me.nombre_completo <> Null) Then
If (Me.sueldo <> 0) Then
dbs.Execute "INSERT INTO SueldosTempo (fecha_gasto) VALUES (" & _
auxFecha & ");"
auxReg = DMax("id_gasto_detalle", "SueldosTempo")
dbs.Execute "UPDATE SueldosTempo SET id_gasto = " & _
auxGastoId & " WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET cantidad = " & "1" & _
" WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET servicio = ""Sueldo (" & _
Me.nombre_completo & ")"" WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET monto_servicio = " & _
Me.sueldo & " WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET fecha_gasto = " & _
auxFecha & " WHERE id_gasto_detalle = " & auxReg & ";"
ElseIf (Me.bono <> 0) Then
dbs.Execute "INSERT INTO SueldosTempo (fecha_gasto) VALUES (" & _
auxFecha & ");"
auxReg = DMax("id_gasto_detalle", "SueldosTempo")
dbs.Execute "UPDATE SueldosTempo SET id_gasto = " & _
auxGastoId & " WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET cantidad = " & "1" & _
" WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET servicio = ""Bono (" & _
Me.nombre_completo & ")"" WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET monto_servicio = " & _
Me.bono & " WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET fecha_gasto = " & _
auxFecha & " WHERE id_gasto_detalle = " & auxReg & ";"
ElseIf (Me.hrExtra <> 0) Then
dbs.Execute "INSERT INTO SueldosTempo (fecha_gasto) VALUES (" & _
auxFecha & ");"
auxReg = DMax("id_gasto_detalle", "SueldosTempo")
dbs.Execute "UPDATE SueldosTempo SET id_gasto = " & _
auxGastoId & " WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET cantidad = " & "1" & _
" WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET servicio = ""HrExtra (" & _
Me.nombre_completo & ")"" WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET monto_servicio = " & _
Me.hrExtra & " WHERE id_gasto_detalle = " & auxReg & ";"
dbs.Execute "UPDATE SueldosTempo SET fecha_gasto = " & _
auxFecha & " WHERE id_gasto_detalle = " & auxReg & ";"
Else
End If
Else
End If
rst.MoveNext
Loop
dbs.Close
Set rst = Nothing
Set db = Nothing
End Sub
I believe you’ll solve your problem by changing this line:
Set db = CurrentDb()toset dbs = CurrentDb()And adding
Option Explicitas your first line in the module.dbs.closeis causing an error because you never assigndbsa value.If you had enabled option explicit while writing this, the IDE would have thrown a compile error letting you know that db was never defined.
Edit:
Also change
db = nothingtodbs = nothing.