【问题标题】:MS Access using UPDATE statement keeps entering new dataMS Access 使用 UPDATE 语句不断输入新数据
【发布时间】: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


【解决方案1】:

Tag 属性默认不是空的吗?如果要保存新记录,则必须将标记属性设置为等于该数字。因此,当您更新记录时,Where number = & me.txt.number.tag 为真。否则“”的所有记录标签将等于“”。

另外,在currentDb.execute 行(then 之后)中添加标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多