【发布时间】:2015-07-17 19:53:43
【问题描述】:
我正在使用 UPDATE 语句,但每当我单击 Edit 按钮然后单击 Update 时,它都会输入一个新行但具有相同的数据。
我的代码:
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
【问题讨论】:
-
在更新语句的正上方输入:
MsgBox "Update Statement Reached",看看它是否会在您期望Update语句运行时弹出。 -
感谢您的评论,但仍然没有任何反应。每当我单击更新按钮时,它都会不断输入新数据。
-
那么,如果 Msgbox 永远不会弹出,您认为会发生什么?
-
我对 vba 代码没有足够的了解,这就是我在这里寻求帮助的原因。我只是在互联网上看到的一些教程视频中复制和编辑了这些代码,但仍然无法正确执行。我的代码有问题,但我无法弄清楚。
标签: ms-access vba ms-access-2010