Whats wrong with this SQL

n2amg
06-11-2012, 10:34 AM
I have a very simple database with 7 fields.
Index-AutuNumber
Callsign - Text
Freq - Double
TS - Text
QSXFreq - Double
Comment - Text
Spotter - Text
Band - Integer

Here is the routine I am using.
Public Sub AddSpot(ByVal Callsgn As String, ByVal Freq As Double, ByVal TS As String, ByVal QSXFreq As Double, ByVal Comments As String, ByVal Spotter As String, ByVal Band As Integer)
Dim SQL As String
Dim objCmd As New OleDb.OleDbCommand
Dim Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & Application.StartupPath & "\FldigiDXCluster.mdb")
If Comments = "" Then Comments = "nothing"
Try
Con.Open()
SQL = "Insert into Spots (Callsign, Freq, TS, QSXFreq, Comments, Spotter, Band) VALUES ('" & Callsgn & "', '" & Freq & "', '" & TS & "', '" & QSXFreq & "', '" & Comments & "', '" & Spotter & "', '" & Band & "')"
objCmd = New OleDb.OleDbCommand(SQL, Con)
objCmd.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
Con.Close()
End Try
End Sub


The result from a breakpoint SQL= this:
?SQL
"Insert into Spots (Callsign, Freq, TS, QSXFreq, Comments, Spotter, Band) VALUES ('EO2012JT', '14189', '1630Z', '0', 'tnx QSO', 'R9UAG', '20')"

But every time the routine is run I get :
Syntax error in INSERT INTO Statement - Microsoft Jet Database Engine.

I have tried with and without ' around fields with numbers but I cannot figure it out..

TIA
Rick

Flyguy
06-11-2012, 12:24 PM
The variables for the column names are embedded withing the quotes of your query.
So the variable names are used instead of the variable values.

n2amg
06-11-2012, 01:42 PM
I have changed the variable names by adding a 1 to them but am still getting the syntax error message as soon as the routine is run..
Public Sub AddSpot(ByVal Callsgn As String, ByVal Freq1 As Double, ByVal TS1 As String, ByVal QSXFreq1 As Double, ByVal Comments1 As String, ByVal Spotter1 As String, ByVal Band1 As Integer)
Dim SQL As String
Dim objCmd As New OleDb.OleDbCommand
Dim Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & Application.StartupPath & "\FldigiDXCluster.mdb")
If Comments1 = "" Then Comments1 = "nothing"
Try
Con.Open()
SQL = "Insert into Spots (Callsign, Freq, TS, QSXFreq, Comments, Spotter, Band) VALUES ('" & Callsgn & "', " & Freq1 & ", '" & TS1 & "', " & QSXFreq1 & ", '" & Comments1 & "', '" & Spotter1 & "', " & Band1 & ")"
objCmd = New OleDb.OleDbCommand(SQL, Con)
objCmd.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message & " - " & ex.Source)
Con.Close()
End Try
End Sub

Flyguy
06-11-2012, 02:00 PM
Yes, my mistake. I did not read your query correct.

Maybe a problem with the column names?

n2amg
06-11-2012, 07:41 PM
I have checked the columns about 10 times and made sure the type of variable was correct in each column. This just totally baffles me... The only thing I have not done yet was delete the table and create another one...

n2amg
06-11-2012, 08:48 PM
Well after doing some more testing. I went thru and added each field to the SQL and it added the data fine until I got to this: , " & Band1 & ")"

Once it hits that it gives the syntax error in INSERT INTO error message. Band1 is a integer and the db column is set to use integer for it's value.

Flyguy
06-12-2012, 01:35 AM
Can you try the query directly from within MS-Access?

n2amg
06-14-2012, 06:30 PM
I finally figured it out. For some reason Access did not like the column named Band. Once I changed the name to something else it worked fine... Go figure..
Thanks
Rick

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum