运行时错误:您输入的值对此字段无效

时间:2015-10-27 22:43:06

标签: vba ms-access-2010

我正在尝试使用Subform更新记录。当我第一次更新它正确更新但当我尝试再次更新同一记录时,我收到错误:

  

运行时错误' -2147352567(80020009)':您输入的值对此字段无效

以下是表格。

Form

单击编辑时,所选记录中的信息将填充到相应的文本框中。更新信息并单击“更新”后,记录将首次成功更新。

当我尝试再次更新同一条记录时,我得到了上述错误。

Error

这是在单击编辑时运行的VB脚本。

Private Sub cmdEdit_Click()
    'Check if data exists in the list
    If Not (Me.frmschoolsub.Form.Recordset.EOF And Me.frmschoolsub.Form.Recordset.BOF) Then
        'get data to text box control
        With Me.frmschoolsub.Form.Recordset
            Me.Schooltxt = .Fields("School_Name")
            Me.Desctxt = .Fields("Description")
            Me.Deantxt = .Fields("Dean")
            Me.Adeantxt = .Fields("Associate_Dean")
            'store id of student in tag
            Me.Schooltxt.Tag = .Fields("School_ID")
            'change caption of button to update
            Me.cmdAdd.Caption = "Update"
            Me.cmdEdit.Enabled = False
        End With
    End If
End Sub

当我点击Debug时,它突出显示以下行。

Me.Schooltxt = .Fields("School_Name")

你能帮我确定一下这里的问题吗?

2 个答案:

答案 0 :(得分:1)

我认为每次更新后,我都会失去记录的位置。我在update和Requery之后添加了以下语句

Me.frmschoolsub.Form.Recordset.MoveFirst

以下是代码段。

    Else
        CurrentDb.Execute "Update School " & _
                " SET School_Name ='" & Me.Schooltxt & "'" & _
                ", Description ='" & Me.Desctxt & "'" & _
                ", Dean ='" & Me.Deantxt & "'" & _
                ", Associate_Dean='" & Me.Adeantxt & "'" & _
                "where School_ID=" & Me.Schooltxt.Tag


    End If
    'Clear the Fields
    cmdClr_Click
    'Refresh the table
    frmschoolsub.Form.Requery
    Me.frmschoolsub.Form.Recordset.MoveFirst

这解决了这个问题。

答案 1 :(得分:0)

发生这样的错误,表单字段无法像您一样引用文本框。

您可以按照以下方式执行此操作。

With Me.frmschoolsub.Form.Recordset
     Me.Schooltxt = Forms![<<if you have main form  >>]![frmschoolsub].form![School_Name]