从 DLookup Multiple Criteria VBA 中查找并替换 Table 中的值

Find and Replace Value in Table From DLookup Multiple Criteria VBA

我开始在 Access 中进行编程,我真的需要帮助!!

我的objective是在"tbCustoProjeto"table中创建一个运行的模块,根据Dlookup重写字段"Valor HH"的值。我找到了一些解决方案(通过 azurous),我认为可以解决这个问题,但是当我 运行 代码时,返回

"object-required-error".

Sub redefineHH()
Dim objRecordset As ADODB.Recordset
Set objRecordset = New ADODB.Recordset
Dim i As Integer
Dim value As Variant
Dim HHTotal As Double
Set HHTotal = DLookup("[CustoTotalNivel]", "tbNivelNome2", "nUsuario='" & tbCustoProjeto!NumUsuario & "'" & "AND Numeric<=" & tbCustoProjeto!DataNumero)

'initated recordset obejct
objRecordset.ActiveConnection = CurrentProject.Connection
Call objRecordset.Open("tbCustoProjeto", , , adLockBatchOptimistic)
'find the target record
While objRecordset.EOF = False
'If objRecordset.Fields.Item(13).value > 0 Then
objRecordset.Fields.Item(13).value = HHTotal
objRecordset.UpdateBatch
'exit loop
'objRecordset.MoveLast
objRecordset.MoveNext
'End If
Wend
MsgBox ("Pesquisa Finalizada")
End Sub

tbCustoProjeto 打印

tbNivelNome2 的打印

拜托,谁能告诉我错误在哪里?我不知道该怎么办。

不能像动态参数那样直接引用 table。 DLookup 应该从记录集中和循环内提取动态条件。不要对数字类型字段参数使用撇号分隔符。

删除不必要的串联。

Sub redefineHH()
Dim objRecordset As ADODB.Recordset
Set objRecordset = New ADODB.Recordset
objRecordset.Open "tbCustoProjeto", CurrentProject.Connection, , adLockBatchOptimistic
While objRecordset.EOF = False
    objRecordset.Fields.Item(13) = DLookup("[CustoTotalNivel]", "tbNivelNome2", _
        "nUsuario=" & objRecordset!NumUsuario & " AND Numeric <=" & objRecordset!DataNumero)
    objRecordset.UpdateBatch
    objRecordset.MoveNext
Wend
MsgBox ("Pesquisa Finalizada")
End Sub