关于数据库:在INSERT INTO语句中获取语法错误

Getting a Syntax error in INSERT INTO statement

我需要帮助弄清楚为什么在尝试在Microsoft Access中运行此代码时出现此语法错误。我正在这个数据库上工作,并且有一些经验,但是还不是很多。

我在当前正在使用的数据库之前创建了一个数据库,它运行良好。它与我现在正在使用的数据库非常相似,因此我只复制了它,然后重命名了字段和文本框以及所有与该数据库将处理的信息相匹配的内容。

本质上,我有一个包含数据的表,然后我想要一个表单,该表单的表中每个字段都有一个文本框,并将主表的子表合并到表单中。用户使用信息填充文本框,然后将其添加到表中,或者他们可以单击记录并对其进行编辑或使用表单上的相应按钮将其删除。

现在,我遇到运行时错误" 3134":尝试单击我的一个表单上的添加按钮时,INSERT INTO语句中出现语法错误。如果设置为更新,则只会在添加按钮上发生,因此会在更新按钮上发生,但是所有其他按钮都可以正常工作。

这是新数据库的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Option Compare Database

Private Sub cmdAdd_Click()
    'when we click on button Add there are two options
    '1. for insert
    '2. for update
    If Me.txtICN.Tag &"" ="" Then
        'this is for insert new
        'add data to table
        CurrentDb.Execute"INSERT INTO tblInventory(ICN, manu, model, serial, desc, dateRec, dateRem, dispo, project, AMCA, UL, comments)" & _
       " VALUES(" & Me.txtICN &", '" & Me.txtManu &"', '" & Me.txtModel &"', '" & Me.txtSerial &"', '" & Me.txtDesc &"', '" & Me.txtDateRec &"', '" & Me.txtDateRem &"', '" & Me.txtDispo &"', '" & Me.txtProject &"', '" & Me.txtAMCA &"', '" & Me.txtUL &"', '" & Me.txtComments &"')"
    Else
        'otherwise (Tag of txtICN store the ICN of item to be modified)
        CurrentDb.Execute"UPDATE tblInventory" & _
       " SET ICN =" & Me.txtICN & _
       ", manu = '" & Me.txtManu &"'" & _
       ", model = '" & Me.txtModel &"'" & _
       ", serial = '" & Me.txtSerial &"'" & _
       ", desc = '" & Me.txtDesc &"'" & _
       ", dateRec = '" & Me.txtDateRec &"'" & _
       ", dateRem = '" & Me.txtDateRem &"'" & _
       ", dispo = '" & Me.txtDispo &"'" & _
       ", project = '" & Me.txtProject &"'" & _
       ", AMCA = '" & Me.txtAMCA &"'" & _
       ", UL = '" & Me.txtUL &"'" & _
       ", comments = '" & Me.txtComments &"'" & _
       " WHERE ICN =" & Me.txtICN.Tag
    End If

    'clear form
    cmdClear_Click
    'refresh data in list on form
    frmInventorySub.Form.Requery
End Sub

Private Sub cmdClear_Click()
    Me.txtICN =""
    Me.txtManu =""
    Me.txtModel =""
    Me.txtSerial =""
    Me.txtDesc =""
    Me.txtDateRec =""
    Me.txtDateRem =""
    Me.txtDispo =""
    Me.txtProject =""
    Me.txtAMCA =""
    Me.txtUL =""
    Me.txtComments =""

    'focus on ID text box
    Me.txtICN.SetFocus
    'set button edit to enable
    Me.cmdEdit.Enabled = True
    'change caption of button add to Add
    Me.cmdAdd.Caption ="Add"
    'clear tag on txtICN for reset new
    Me.txtICN.Tag =""
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

Private Sub cmdDelete_Click()
    'delete record
    'check existing selected record
    If Not (Me.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
        'confirm delete
        If MsgBox("Are you sure you want to delete this item?", vbYesNo) = vbYes Then
            'delete now
            CurrentDb.Execute"DELETE FROM tblInventory" & _
               "WHERE ICN =" & Me.frmInventorySub.Form.Recordset.Fields("ICN")
            'refresh data in list
            Me.frmInventorySub.Form.Requery
        End If
    End If
End Sub

Private Sub cmdEdit_Click()
    'check whether there exists data in list
    If Not (Me.frmInventorySub.Form.Recordset.EOF And Me.frmInventorySub.Form.Recordset.BOF) Then
        'get data to text box control
        With Me.frmInventorySub.Form.Recordset
            Me.txtICN = .Fields("ICN")
            Me.txtManu = .Fields("manu")
            Me.txtModel = .Fields("model")
            Me.txtSerial = .Fields("serial")
            Me.txtDesc = .Fields("desc")
            Me.txtDateRec = .Fields("dateRec")
            Me.txtDateRem = .Fields("dateRem")
            Me.txtDispo = .Fields("dispo")
            Me.txtProject = .Fields("project")
            Me.txtAMCA = .Fields("AMCA")
            Me.txtUL = .Fields("UL")
            Me.txtComments = .Fields("comments")
            'store id of item in Tag of txtICN in case ICN is modified
            Me.txtICN.Tag = .Fields("ICN")
            'change caption of button add to Update
            Me.cmdAdd.Caption ="Update"
            'disable button edit
            Me.cmdEdit.Enabled = False
        End With
    End If
End Sub

这是旧数据库的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Option Compare Database

Private Sub cmdAdd_Click()
    'when we click on button Add there are two options
    '1. for insert
    '2. for update
    If Me.txtID.Tag &"" ="" Then
        'this is for insert new
        'add data to table
        CurrentDb.Execute"INSERT INTO tblEquipmentList(equipID, equipDesc, equipManu, equipModelNum, equipSerNum, lastCalDate, calDue)" & _
       " VALUES(" & Me.txtID &", '" & Me.txtDesc &"', '" & Me.txtManu &"', '" & Me.txtModelNum &"', '" & Me.txtSerNum &"', '" & Me.txtLastCalDate &"', '" & Me.txtCalDueDate &"')"
    Else
        'otherwise (Tag of txtID store the id of equipment to be modified)
        CurrentDb.Execute"UPDATE tblEquipmentList" & _
       " SET equipID =" & Me.txtID & _
       ", equipDesc = '" & Me.txtDesc &"'" & _
       ", equipManu = '" & Me.txtManu &"'" & _
       ", equipModelNum = '" & Me.txtModelNum &"'" & _
       ", equipSerNum = '" & Me.txtSerNum &"'" & _
       ", lastCalDate = '" & Me.txtLastCalDate &"'" & _
       ", calDue = '" & Me.txtCalDueDate &"'" & _
       " WHERE equipID =" & Me.txtID.Tag
    End If

    'clear form
    cmdClear_Click
    'refresh data in list on form
    frmEquipmentListSub.Form.Requery
End Sub

Private Sub cmdClear_Click()
    Me.txtID =""
    Me.txtDesc =""
    Me.txtManu =""
    Me.txtModelNum =""
    Me.txtSerNum =""
    Me.txtLastCalDate =""
    Me.txtCalDueDate =""

    'focus on ID text box
    Me.txtID.SetFocus
    'set button edit to enable
    Me.cmdEdit.Enabled = True
    'change caption of button add to Add
    Me.cmdAdd.Caption ="Add"
    'clear tag on txtID for reset new
    Me.txtID.Tag =""
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub

Private Sub cmdDelete_Click()
    'delete record
    'check existing selected record
    If Not (Me.frmEquipmentListSub.Form.Recordset.EOF And Me.frmEquipmentListSub.Form.Recordset.BOF) Then
        'confirm delete
        If MsgBox("Are you sure you want to delete this piece of equipment?", vbYesNo) = vbYes Then
            'delete now
            CurrentDb.Execute"DELETE FROM tblEquipmentList" & _
               "WHERE equipID =" & Me.frmEquipmentListSub.Form.Recordset.Fields("equipID")
            'refresh data in list
            Me.frmEquipmentListSub.Form.Requery
        End If
    End If
End Sub

Private Sub cmdEdit_Click()
    'check whether there exists data in list
    If Not (Me.frmEquipmentListSub.Form.Recordset.EOF And Me.frmEquipmentListSub.Form.Recordset.BOF) Then
        'get data to text box control
        With Me.frmEquipmentListSub.Form.Recordset
            Me.txtID = .Fields("equipID")
            Me.txtDesc = .Fields("equipDesc")
            Me.txtManu = .Fields("equipManu")
            Me.txtModelNum = .Fields("equipModelNum")
            Me.txtSerNum = .Fields("equipSerNum")
            Me.txtLastCalDate = .Fields("lastCalDate")
            Me.txtCalDueDate = .Fields("calDue")
            'store id of equipment in Tag of txtID in case id is modified
            Me.txtID.Tag = .Fields("equipID")
            'change caption of button add to Update
            Me.cmdAdd.Caption ="Update"
            'disable button edit
            Me.cmdEdit.Enabled = False
        End With
    End If
End Sub

如您所见,我非常有信心除了字段名称外,它们几乎相同。

我还在这里链接数据库的屏幕快照专辑:http://imgur.com/a/xLV3Q

感谢您提供的任何帮助。


问题可能是:
新表tblInventory的列DESC是SQL保留关键字。 您有两种选择:

  • 删除列DESC并创建一个其他名称为OR的新列;
  • 将方括号添加到脚本中,如下所示:INSERT INTO tblInventory([ICN], [manu], [model], [serial], [desc], [dateRec], [dateRem], [dispo], [project], [AMCA], [UL], [comments])
  • 请检查SQL保留关键字的完整列表:保留关键字-Transact-SQL