访问 VBA 对象必需错误,为 IF 语句引用 Table 中的字段

Access VBA Object Required Error, referencing Field in a Table for IF statement

我遇到了一个需要对象的错误,因为它非常模糊,所以我无法修复它。我想说的是:如果该记录的 AssocID 字段为空,则执行 SQL 语句等。如果不是,则显示 MsgBox 并转到下一条记录。这是我的代码。

Private Sub StartButton_Click()
    DoCmd.SetWarnings False
    Dim mySQL As String
    GetID = Forms!frm_MainMenu!AssocIDBox
    CurRecord = Forms!frm_EC_All![Loan#].Value
    mySQL = "UPDATE tbl_Data SET tbl_Data.[AssocID] = " & GetID & " , tbl_Data.[tsStartAll] = Now WHERE tbl_Data.[Loan#] = " & CurRecord
    '
    '
    '
    If IsNull(tbl_Data.AssocID.Value) Then
        DoCmd.RunSQL mySQL
        DoCmd.SetWarnings True
        Me.TimerActivatedLabel.visible = True
        Me.Refresh
        ClaimedMsg = MsgBox("Loan has been assigned to you.", vbOKOnly, "Loan Assigned")
            If ClaimedMsg = vbOK Then
                DoCmd.SetWarnings False
                DoCmd.RunSQL "UPDATE tbl_Data SET tbl_Data.[tsHaveDocs] = Now WHERE tbl_Data.[Loan#] = " & CurRecord
                DoCmd.SetWarnings True
            End If
    Else
        AlreadyAssignedMsg = MsgBox("Loan has already been assigned.  Press OK to move to the next loan.", vbOKOnly, "Already Assigned!")
            If AlreadyAssignedMsg = vbOKOnly Then
                DoCmd.GoToRecord , , acNext
            End If
    End If
End Sub

错误发生在 If IsNull(etc) 行。我看过很多其他的东西,但要么不适用,要么我真的不明白他们在说什么。我还是个初学者。谢谢!

如果出于某种奇怪的原因,您没有 tbl_Data.AssocID 字段作为表单中文本框的控制源,您可以这样做:

...
' Declare a string variable and set it to the value of the field
' I concatenate an empty string to be sure to have not a null
Dim AssocID as string
AssocID = Dlookup("AssocID","tbl_Data","[Loan#] = " & CurRecord) & ""

' Test the string variable
If AssocID = "" Then
...