gpt4 book ai didi

sql - Access INSERT INTO SQL 错误

转载 作者:行者123 更新时间:2023-12-04 23:48:52 25 4
gpt4 key购买 nike

插入 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

最佳答案

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

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

保留字

Jet
ACE

关于sql - Access INSERT INTO SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11886990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com