1

我不是访问和/或编程方面的专家;所以,很确定我的问题会有点愚蠢..但是,我需要你的帮助,所以..我敢来问:)

所以,事情就是这样:我有一个表单,当它关闭时,它将通过 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
4

1 回答 1

2

Set db = CurrentDb()我相信你会通过改变这一行来解决你的问题: set dbs = CurrentDb()

并添加Option Explicit为模块中的第一行。

dbs.close导致错误,因为您从未分配dbs值。

如果您在编写此代码时启用了显式选项,IDE 将引发编译错误,让您知道 db 从未定义。

编辑:

db = nothing改为dbs = nothing.

于 2012-08-28T22:34:19.000 回答