VBA代码的重复记录运行时错误

时间:2016-07-15 13:57:30

标签: database vba ms-access ms-access-2013

我目前在Access 2013中工作。

我遇到的问题是我试图将主窗体中的记录复制到带有按钮的子窗体。

我的表包括:

tbl40_1                      tbl40_1_changes

ID (PK)                      ID (PK)
System_Name                  40_1_ID (FK)
Configuration_Type           System_Name
Configuration_ID             Configuration_Type
Reference_Document           Configuration_ID 
Approval_Authority           Reference_Document
Approval_Mechanism           Approval_Authority
Item_Location                Approval_Mechanism 
Custodian                    Item_Location  
                             Custodian              

我对该按钮的当前代码包括:

Private Sub Duplicate_Click()

Dim strInsert
Dim db As DAO.Database

StrInsert = "INSERT INTO tbl40_1_changes_subform (ID, [System_Name], [Configuration_Type], [Configuration_ID], [Reference_Document], [Approval_Authority], [Approval_Mechanism], [Item_Location], [Custodian]" & _
            "SELECT ID, [System_Name], [Configuration_Type], [Configuration_ID], [Reference_Document], [Approval_Authority], [Approval_Mechanism], [Item_Location], [Custodian]" & _
            "FROM frm40_1" & _
            "WHERE [40_1_ID] = " & Me.tbl40_1_changes_subform.Form.txt40_1_ID

Set db = CurrentDb()

db.Execute strInsert, dbFailOnError
Set db = Nothing


End Sub

当我点击按钮时,我收到[StrInsert =" INSERT INTO"," Select"," From",&}的运行时错误#34;其中&#34]。我不确定这段代码中有什么问题,因为它突出了整段代码。

1 个答案:

答案 0 :(得分:1)

您的换行符缺少空格。

添加Debug.Print StrInsert即可查看。

而且,正如Hynek Bernard所写,INSERT条款缺少结束)

尝试

StrInsert = "INSERT INTO tbl40_1_changes (ID, [System_Name], [Configuration_Type], [Configuration_ID], [Reference_Document], [Approval_Authority], [Approval_Mechanism], [Item_Location], [Custodian])" & _
            " SELECT ID, [System_Name], [Configuration_Type], [Configuration_ID], [Reference_Document], [Approval_Authority], [Approval_Mechanism], [Item_Location], [Custodian]" & _
            " FROM frm40_1" & _
            " WHERE [40_1_ID] = " & Me.tbl40_1_changes_subform.Form.txt40_1_ID

Debug.Print StrInsert   ' Ctrl+G shows Debug output

修改

我认为您的WHERE子句非常关闭。您正在从frm40_1(在主窗体上)复制到子窗体上的tbl40_1_changes,对吗?

如果tbl40_1_changes.ID是自动编号,则无法设置它。

然后你需要像

这样的东西
StrInsert = "INSERT INTO tbl40_1_changes (40_1_ID, [System_Name], [Configuration_Type], [Configuration_ID], [Reference_Document], [Approval_Authority], [Approval_Mechanism], [Item_Location], [Custodian])" & _
            " SELECT ID, [System_Name], [Configuration_Type], [Configuration_ID], [Reference_Document], [Approval_Authority], [Approval_Mechanism], [Item_Location], [Custodian]" & _
            " FROM tbl40_1" & _
            " WHERE ID = " & Me.ID