Microsoft Access VBA运行时错误2465

时间:2015-06-19 11:38:25

标签: vba ms-access access-vba runtime-error ms-access-2010

我目前正在使用表单来更新数据库中的字段。按钮(cmdFind)用于查找部件#的记录(输入文本框txtFindPart),然后将数据填充到In1-52和out1-52中。当我运行它时,我得到Run-time Error 2465 Microsoft Access can not find the field '|1' referred to in your expression.

Private Sub cmdFind_Click()
    Dim i As Integer
    i = 1
    If IsNull(txtFindPart) = False Then
        If Me.Recordset.NoMatch Then
            MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
            Me!txtFindPart = Null
        End If
        Do Until i = 53
            Me.Controls("in" & i) = DLookup("[In-Week " & i & "]", [Parts], "(([Parts].[Part #]) = '" & txtFindPart & "')")
            Me.Controls("out" & i) = DLookup("[Out-Week " & i & "]", [Parts], "(([Parts].[Part #]) = '" & txtFindPart & "')")
            i = i + 1
        Loop
    End If
End Sub

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为问题在于表Parts未打开且导致错误。我只需要添加一行来打开表格,最后关闭它。

DoCmd.OpenTable "Parts"
Dim i As Integer
i = 1
If IsNull(txtFindPart) = False Then
    If Me.Recordset.NoMatch Then
        MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
        Me!txtFindPart = Null
    End If
    Do Until i = 53
        Me.Controls("in" & i) = DLookup("[In-Week " & i & "]", "[Parts]", "(([Parts].[Part #]) = '" & txtFindPart & "')")
        Me.Controls("out" & i) = DLookup("[Out-Week " & i & "]", "[Parts]", "(([Parts].[Part #]) = '" & txtFindPart & "')")
        i = i + 1
    Loop
End If
DoCmd.Close , "Parts"