获取运行时错误' 3075'查询表达式

时间:2017-09-29 15:14:51

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

我正在尝试在ms访问中构建一个库管理数据库。当我填写表单并单击添加然后我得到运行时错误3075.你能帮我找出问题吗?提前谢谢。

 Private Sub cmdAdd_Click()

'add data to table

Dim strInsert As String
strInsert = "INSERT INTO Library (ID_Label, [Book Name_Label], Author_Label, Publishers_Label, Category_Label, Year_Label, [Book type_Label], [Page Count_Label], [Unique ID_Label], [Shelf Number_Label], Comments_Label, [Lend To_Label], [Phone Number_Label]) " & _
" VALUES(" & Me.txtId & ",'" & Me.txtBookName & ",'" & Me.txtAuthor & ",'" & Me.txtPublishers & ",'" & Me.cmbCategory & ",'" & Me.cmbYear & ",'" & Me.cmbBookType & ",'" & Me.txtPageCount & ",'" & Me.txtUniqueId & ",'" & Me.txtShelfNumber & ",'" & Me.txtComments & ",'" & Me.txtLendTo & ",'" & Me.txtPhoneNumber & "')"
Debug.Print strInsert,
CurrentDb.Execute strInsert, dbFailOnError

'refresh data in list on form
frmRecordNewSub.Form.Requery

2 个答案:

答案 0 :(得分:0)

这:'" & Me.txtComments & ",件应该是'" & Me.txtComments & "',

如果您希望我们调试动态创建的SQL字符串,请Debug.Print它(这个很简单)。

如果要避免此类错误(和SQL注入),请使用参数。

答案 1 :(得分:0)

实际上,几乎每个输入值都有不成对的单引号。

试试这个:

Private Sub cmdAdd_Click()

'add data to table

Dim strInsert As String
strInsert = "INSERT INTO Library (ID_Label, [Book Name_Label], Author_Label, Publishers_Label, Category_Label, Year_Label, [Book type_Label], [Page Count_Label], [Unique ID_Label], [Shelf Number_Label], Comments_Label, [Lend To_Label], [Phone Number_Label]) " & _
" VALUES(" & Me.txtId & ", '" & Me.txtBookName & "', '" & Me.txtAuthor & "', '" & Me.txtPublishers & "', '" & Me.cmbCategory & "', '" & Me.cmbYear & "', '" & Me.cmbBookType & "', '" & Me.txtPageCount & "', '" & Me.txtUniqueId & "', '" & Me.txtShelfNumber & "', '" & Me.txtComments & "', '" & Me.txtLendTo & "', '" & Me.txtPhoneNumber & "')"
Debug.Print strInsert,
CurrentDb.Execute strInsert, dbFailOnError

'refresh data in list on form
frmRecordNewSub.Form.Requery