riley3131
riley3131

Reputation: 280

Access INSERT INTO SQL Error

Having trouble with my insert into sql. All the code below works, excep the SQL strings that add a value for level. Here is my onclick code for a form with three boxes. The table that stores the pH, Temp, and Level values has a number format with field size 'single' set for all three fields.

Again, if I leave the 'level' box blank, it works! But when I try to insert a level value, with any other combination of values, it fails.

Code Below:

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

Upvotes: 3

Views: 1290

Answers (1)

Fionnuala
Fionnuala

Reputation: 91356

Level is a reserved word and must be enclosed in square brackets.

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

Reserved words

Jet
ACE

Upvotes: 4

Related Questions