2015-07-13 67 views
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 
+1

在你的Update語句的正上方放置這個:'MsgBox「Update Statement Reached'',看看它是否在你期望'Update'語句運行時彈出。 – Newd

+0

感謝您的評論,但仍然沒有任何反應。無論何時點擊更新按鈕,它都會繼續輸入新數據。 – User033

+0

那麼如果Msgbox永遠不會彈出,你會怎麼想呢? – Newd

回答

0

是不是Tag屬性默認爲空?如果您要保存新記錄,則必須將標記屬性設置爲等於該數字。所以當你來更新記錄時,Where number = & me.txt.number.tag是正確的。否則,「」的所有記錄標籤將等於「」。

此外,在currentDb.execute行(在then之後)的選項卡中。