访问INSERT INTO SQL错误

时间:2012-08-09 15:41:27

标签: sql ms-access

我插入sql时遇到问题。除了为级别添加值的SQL字符串之外,下面的所有代码都有效。这是我的onclick代码,用于包含三个框的表单。存储pH,温度和水平值的表格具有字段大小'单个'为所有三个字段设置。

再次,如果我离开'级别'盒子空白,它的作品!但是当我尝试插入一个级别值,以及任何其他值组合时,它会失败。

以下代码:

Private Sub SubmitSampleData_Click()
Dim pH, Temp, Level As Single
Dim TodaysDate As Date

TodaysDate = Date

If IsNull(Me.pH.Value) Then
    pH = 1
Else
    pH = Me.pH.Value
End If
If IsNull(Me.Temp.Value) Then
    Temp = 1
Else
    Temp = Me.Temp.Value
End If
If IsNull(Me.Level.Value) Then
    Level = 1
Else
Level = Me.Level.Value
End If

If Level = 1 And Temp = 1 And pH = 1 Then
     Exit sub
ElseIf Level = 1 And Temp = 1 Then
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, pH) VALUES (#" & TodaysDate & "#, " & pH & ")"
ElseIf Temp = 1 And pH = 1 Then
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, Level) VALUES (#" & TodaysDate & "#, " & Level & ")"
ElseIf Level = 1 And pH = 1 Then
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, Temp) VALUES (#" & TodaysDate & "#, " & Temp & ")"
ElseIf Level = 1 Then
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, pH, Temp) VALUES (#" & TodaysDate & "#, " & pH & ", " & Temp & ")"
ElseIf Temp = 1 Then
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, pH, Level) VALUES (#" & TodaysDate & "#, " & pH & ", " & Level & ")"
ElseIf pH = 1 Then
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, Temp, Level) VALUES (#" & TodaysDate & "#, " & Temp & ", " & Level & ")"
Else    
    CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, pH, Temp, Level) VALUES (#" & TodaysDate & "#, " & pH & ", " & Temp & ", " & Level & ")"
End If

MsgBox "Submit Successful!", vbOKOnly, "Success!"

Me.pH.Value = Null
Me.Temp.Value = Null
Me.Level.Value = Null
End Sub

1 个答案:

答案 0 :(得分:4)

级别是保留字,必须用方括号括起来。

CurrentDb.Execute "INSERT INTO SampleDataQuery (DateCreated, [Level]) VALUES (#" _
   & TodaysDate & "#, " & Level & ")"

保留字

Jet
ACE