2015-10-27 96 views
0

我正嘗試使用子窗體更新記錄。當我第一次更新它時,它會正確更新,但是當我嘗試再次更新相同的記錄時,我收到錯誤:運行時錯誤:您輸入的值對此字段無效

Run-time error '-2147352567 (80020009)': The value you entered isn't valid for this field

以下是表單。

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 

當我點擊調試它突出顯示以下行。

Me.Schooltxt = .Fields("School_Name") 

你能幫我找出這裏有什麼問題。

+0

當我調整我注意到,在第一個塊你有'Me.Schooltxt.Tag =點域(「學校ID」)'代碼的縮進,你有'調試線Me.Schooltxt =點域( 「School_Name」)'。哪個是對的? – theB

+0

是的,我使用該標籤來存儲ID以用於更新。 – KraZy

+0

道歉,我讀了'School_ID'和'School_Name'作爲同一個詞。這實際上是非常有意義的。 – theB

回答

1

我想,每次更新後,我都失去了記錄的位置。我在更新後添加了以下聲明和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 

這解決了這個問題。

0

發生此錯誤的是表單字段無法像您那樣引用到文本框。

你可以這樣做,如下所示。

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