Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting a syntax error when I try to update my table. I am new to Access and can't seem to find the problem with a lot of research and book reading. Access 2016 and Visual studio 2015 , using visual basic.

What I have tried:

VB
Access.ExecQuery("INSERT INTO Log (LDate, LTime, HCall, " &
            "State, County, Band, Freq, Mode, MCall, HRST, MRST, " &
           "HOper, MOper, RunStart, RunEnd, NetDuration, HomeCounty, CountyLine) " &
          "VALUES(@LDate,@LTime,@HCall,@State,@County,@Band,@Freq,@Mode,@MCall,@HRST,@MRST,@Hoper,@Moper,@RunStart,@RunEnd,@NetDuration,@HomeCounty,@CountyLine);")

this is the code I have that takes the parameter and places it into the column. Please help.
Posted
Updated 27-Jan-17 20:42pm
v2
Comments
CHill60 27-Jan-17 13:42pm    
What is the exact error?
Richard Deeming 27-Jan-17 13:42pm    
What's the full error message?
medic63 27-Jan-17 15:19pm    
syntax error in INSERT INTO statement

this si the whole error message
[no name] 27-Jan-17 13:48pm    
I don't get any syntax error but I used Access 2013.
Michael_Davies 27-Jan-17 14:18pm    
Does Access.ExecQuery actually execute the query? If it does then normally you build and SQL parameter string with @<name> placeholders for parameter values then use a parameter Add method to submit the values using the @<names> and only then you execute the query.

Not seen this method, usually use ADO to access Access.

1 solution

Try this:

VB
Private Sub AddLogEntry()
Using DBCon As New OleDbConnection(<PUT ACCESS DB CONNECTION STRING HERE>),
DBCmd As New OleDbCommand("",DBCon)

 DBCmd.CommandText = "INSERT INTO Log (LDate, LTime, HCall, " &
"State, County, Band, Freq, Mode, MCall, HRST, MRST, " &
"HOper, MOper, RunStart, RunEnd, NetDuration, HomeCounty, CountyLine) " &
"VALUES(@LDate,@LTime,@HCall,@State,@County,@Band,@Freq,@Mode,@MCall,@HRST,@MRST,@Hoper,@Moper,@RunStart,@RunEnd,@NetDuration,@HomeCounty,@CountyLine);"

 With DBCmd
 .Parameters.AddWithValue("@LDate", lblDate.Text)
 .Parameters.AddWithValue("@LTime", lblClock.Text)
 .Parameters.AddWithValue("@HCall", txtCall.Text)
 .Parameters.AddWithValue("@State", cbxState.Text)
 .Parameters.AddWithValue("@County", cbxCounty.Text)
 .Parameters.AddWithValue("@Band", cbxBand.Text)
 .Parameters.AddWithValue("@Freq", cbxFreq.Text)
 .Parameters.AddWithValue("@Mode", cbxMode.Text)
 .Parameters.AddWithValue("@Mcall", lblMCall.Text)
 .Parameters.AddWithValue("@HRST", cbxHRst.Text)
 .Parameters.AddWithValue("@MRST", cbxMRst.Text)
 .Parameters.AddWithValue("@Hoper", cbxHOper.Text)
 .Parameters.AddWithValue("@Moper", cbxMOper.Text)
 .Parameters.AddWithValue("@RunStart", txtRStart.Text)
 .Parameters.AddWithValue("@RunEnd", txtREnd.Text)
 .Parameters.AddWithValue("@NetDuration", lblNetDur.Text)
 .Parameters.AddWithValue("@HomeCounty", lblHomeCounty.Text)
 .Parameters.AddWithValue("@CountyLine", cbxCountyL.Text)
 End with

 DBCon.Open()
 if DBCmd.ExecuteNonQuery() = 1 Then
  MsgBox("Row was added SUCCESFULLY.")

  Me.RefreshGrid()
 Else
  MsgBox("Insert FAILED.")
 Endif
 DBCon.Close()
End Using

End Sub
 
Share this answer
 
v2
Comments
medic63 28-Jan-17 11:56am    
Michael_Davies,

That did eliminate the error message, however the file is not added to the database, and don't see the MsgBox. I do see now what you were asking in the previous question. I also see where my formatting of the AddWithValue statement was wrong.

I appreciate your answer it makes the process clearer.
Michael_Davies 28-Jan-17 12:10pm    
I've added an else condition to display failure.

Most problems I had using an Access DB occurred around date and time fields, they must be in the correct format or the whole insert is rejected. Notice you have date and time fields and you get their content from labels if your naming convention reads right, check them for correct format.

You also have what looks like ComboBoxes, have you made sure that an item has been selected on the comboboxes?

Are any of your tables fields required and cannot be empty?
medic63 28-Jan-17 14:05pm    
I added the failure message it doesn't show either. So I am assuming the error is in my refreshGrid sub ? there are 3 clock fields, and a stop watch running to keep track of how long you are on the air. They are all set by UTCNow and the Date field is also set by UTCNow, except the stop watch which is just a counting timer. When I run the program they all appear to be in the correct format. The combo box's are selected as state must be picked to show the county's in that state to be picked from and then a county is picked show it is showing in the combo box. The only field that cannot be empty is the ID field which is set to integer and auto increment it is the very first field and was set by Access.
Hope this answer's your question.
Michael_Davies 28-Jan-17 14:10pm    
If your RefreshGrid is called then you should see the msgbox, use the debugger to walk through and see what is happening.

Is the date field a date field or a string to store a date?

Show me the date you are storing as it appears in lbldate.text.
medic63 28-Jan-17 15:49pm    
The Date field is a string storing a date, and the lbldate.text displays 01/28/2017. Will go thru the debugger after I figure it out I am reading in Visual basic 2015 book. The Times are the same saved as string's like 00:00:00.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900