插入 sql 时遇到问题。下面的所有代码都有效,除了为 level 添加值的 SQL 字符串。这是我的带有三个框的表单的 onclick 代码。存储 pH、Temp 和 Level 值的表具有数字格式,所有三个字段的字段大小都设置为“单个”。
同样,如果我将“级别”框留空,它会起作用!但是,当我尝试使用任何其他值组合插入级别值时,它会失败。
下面的代码:
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