Microsoft Access VBA 插入错误
Posted
技术标签:
【中文标题】Microsoft Access VBA 插入错误【英文标题】:Microsoft Access VBA Insert Into Error 【发布时间】:2015-06-22 17:08:27 【问题描述】:问题:我不断收到以下语句的语法错误:
CurrentDb.Execute "INSERT INTO Inventory ([Part #], [Year], [Week], [In], [Out]) " _
& "VALUES ('" & Me.txtFindPart & "'," & Me.txtYear & "," & i & "," _
& Me.Controls("in" & i) & "," & Me.Controls("out" & i) & ");"
现在我有一种感觉,是 Me.Controls
函数不断抛出此错误,但我无法让它消失。
上下文:这是一个更新按钮,它检查是否有记录,表单中的两个空格是否为空和 strong> 如果这两个条件都不满足,它将创建一个新记录。
现在发生这种情况时,文本框 in# 和 out# 中的数据将用于创建新记录。
守则:
Dim i As Integer
i = 1
Do Until i = 53
If DCount("[Part #]", "[Inventory]", "([Inventory].[Part #] = '" & txtFindPart & "' AND [Inventory].[Year] = " & txtYear & " AND [Inventory].[Week] = " & i & ")") > 0 Then
CurrentDb.Execute "UPDATE Inventory SET [In] = '" & Me.Controls("in" & i) & "' WHERE [Part #] = '" & txtFindPart & "' AND [Year] = " & txtYear & " AND [Week] = " & i & ";"
CurrentDb.Execute "UPDATE Inventory SET [Out] = '" & Me.Controls("out" & i) & "' WHERE [Part #] = '" & txtFindPart & "' AND [Year] = " & txtYear & " AND [Week] = " & i & ";"
i = i + 1
ElseIf Me.Controls("in" & i) = Null And Me.Controls("out" & i) = Null Then
i = i + 1
Else
CurrentDb.Execute "INSERT INTO Inventory ([Part #], [Year], [Week], [In], [Out]) " _
& "VALUES ('" & Me.txtFindPart & "'," & Me.txtYear & "," & i & "," _
& Me.Controls("in" & i) & "," & Me.Controls("out" & i) & ");"
i = i + 1
End If
Loop
Me.Requery
【问题讨论】:
您的表单上是否真的有名为:in1, in2, in3, in4...
和 out1, out2, out3, out4...
的文本框?您是否还会收到特定的语法错误?
运行时错误 '3134' INSERT INTO 语句中的语法错误。
@user2517352:将CurrentDb.Execute
替换为Debug.Print
。这会将您所有的实际 SQL 语句输出到即时窗口中。然后,编辑您的问题并粘贴一两条 SQL 语句,以便我们可以看到实际执行的 SQL。
当我替换代码并单击我的按钮时,它什么也没做。没有执行任何操作,没有更新或添加任何内容,也没有错误消息或弹出窗口。
【参考方案1】:
试试这个:
Dim i As Integer
i = 1
Do Until i = 53
If DCount("[Part #]", "[Inventory]", "[Inventory].[Part #] = '" & txtFindPart & "' AND [Inventory].[Year] = " & txtYear & " AND [Inventory].[Week] = " & i & "") > 0 Then
CurrentDb.Execute "UPDATE Inventory SET [In] = '" & Me("in" & i).Value & "', [Out] = '" & Me("out" & i).Value & "' WHERE [Part #] = '" & txtFindPart & "' AND [Year] = " & txtYear & " AND [Week] = " & i & ";"
ElseIf Not IsNull(Me("in" & i).Value) And IsNull(Me("out" & i).Value) Then
CurrentDb.Execute "INSERT INTO Inventory ([Part #], [Year], [Week], [In], [Out]) " _
& "VALUES ('" & Me.txtFindPart & "'," & Me.txtYear & "," & i & ",'" _
& Me("in" & i).Value & "','" & Me("out" & i).Value & "');"
End If
i = i + 1
Loop
Me.Requery
【讨论】:
运行时错误 3075 查询表达式中的语法错误(缺少运算符)“100,[Out] = '50' WHERE [Part #] = '1' AND [Year] = 2015 AND [Week] = 1;'. 好吧,有了这么少的信息,你让我们猜测。 @Gustav:该错误来自您的答案代码中的UPDATE
查询,因为您在SET [In] = '" & Me("in" & i).Value & "
之后缺少'
感谢克里斯蒂安。编辑;它现在存在。
我想通了。我也没有在需要的地方处理空值。以上是关于Microsoft Access VBA 插入错误的主要内容,如果未能解决你的问题,请参考以下文章
Access 2007,VBA:将 BLOB 插入链接表时出现奇怪的错误