我不是Access和/或编程方面的专家;所以,我很确定我的问题会有点愚蠢......但是,我需要你的帮助,所以......我敢来问问:)
所以,这就是事情:我有一个表单,当它关闭时,将通过VBA代码插入并更新以前创建的表中的一些记录。问题是我已经创建了尽可能接近我所知道的代码来执行此类任务,但我一直在接收来自VBA编译器的错误消息'91':“对象变量或未设置块变量”。
有人可以帮我解决这个问题吗?非常感谢先进;我正在使用的完整代码如下:
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
答案 0 :(得分:2)
我相信您可以通过更改此行来解决您的问题:
Set db = CurrentDb()
至set dbs = CurrentDb()
并在模块中添加Option Explicit
作为第一行。
dbs.close
导致错误,因为您从未为dbs
分配值。
如果您在编写此选项时已启用选项,则IDE将抛出编译错误,让您知道db从未定义过。
编辑:
同时将db = nothing
更改为dbs = nothing
。