1
我正在使用UPDATE語句,但每當我單擊Edit
按鈕,然後Update
時,它將輸入一個新行,但具有相同的數據。使用UPDATE語句的MS Access不斷輸入新數據
我的代碼:
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtNumber.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO tblcompany (companyname, companyaddress, contactnumber, contactperson, emailaddress, website, plantlocation, projectinfo, consultant) " & _
" VALUES('" & Me.txtCompanyName & "','" & _
Me.txtCompanyAddress & "','" & Me.txtContactNumber & "','" & _
Me.txtContactPerson & "','" & Me.txtEmailAddress & "','" & _
Me.txtWebsite & "','" & Me.txtPlantLocation & "','" & _
Me.txtProjectInfo & "','" & Me.txtConsultant & "')"
Else
'otherwise (tag of txtNumber store the number of company to be modified)
CurrentDb.Execute "UPDATE tblcompany " & _
" SET companyname='" & Me.txtCompanyName & "''" & _
", companyaddress='" & Me.txtCompanyAddress & "''" & _
", contactnumber='" & Me.txtContactNumber & "'" & _
", contactperson='" & Me.txtContactPerson & "''" & _
", emailaddress='" & Me.txtEmailAddress & "'" & _
", website='" & Me.txtWebsite & "'" & _
", plantlocation='" & Me.txtPlantLocation & "''" & _
", projectinfo='" & Me.txtProjectInfo & "''" & _
", consultant='" & Me.txtConsultant & "''" & _
" WHERE number=" & Me.txtNumber.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmCompanySub.Form.Requery
End Sub
在你的Update語句的正上方放置這個:'MsgBox「Update Statement Reached'',看看它是否在你期望'Update'語句運行時彈出。 – Newd
感謝您的評論,但仍然沒有任何反應。無論何時點擊更新按鈕,它都會繼續輸入新數據。 – User033
那麼如果Msgbox永遠不會彈出,你會怎麼想呢? – Newd