Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing vb.net application with ms-access.. I am trying to add record but getting this error " Syntax error in INSERT INTO statement. this is my code... please help to resolve....

What I have tried:

Imports System.IO
Imports System.Data.OleDb

Public Class test
Private Access As New DBControl
'Dim accon As OleDbConnection = Access.Connection()
Private accon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=SPSS.accdb;")

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Dim accon As OleDbConnection = Access.Connection()
accon.Open()
Dim accmd As New OleDbCommand("INSERT INTO [empTable] ([empCode], [batch], [empMasterCode], [empName], [empFatherName], [empMotherName], [empVillage], [empParish], [empDeanery], [empDiocese], [empDOBirth], [empDOBaptism], [empQualification], [empBloodGroup], [empContact], [empBasicSalary], [empBrothers], [empSisters], [empAddress], [empDOJoining], [empPOJoining], [empPresentlyStationed], [empStatus], [empDOLeaving], [empDOSuspension], [empDODismissal], [empDORejoining], [empRemarks], [empPhoto] values(@empCode, @batch, @empMasterCode, @empName, @empFatherName, @empMotherName, @empVillage, @empParish, @empDeanery, @empDiocese, @empDOBirth, @empDOBaptism, @empQualification, @empBloodGroup, @empContact, @empBasicSalary, @empBrothers, @empSisters, @empAddress, @empDOJoining, @empPOJoining, @empPresentlyStationed, @empStatus, @empDOLeaving, @empDOSuspension, @empDODismissal, @empDORejoining, @empRemarks, @empPhoto)", accon) With {.CommandType = CommandType.Text}
accmd.Parameters.Add(New OleDbParameter("@empCode", CType(txt1.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@batch", CType(txt2.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empMasterCode", CType(txt3.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empName", CType(txt4.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empFatherName", CType(txt5.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empMotherName", CType(txt6.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empVillage", CType(c1.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empParish", CType(c2.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empDiocese", CType(txt7.Text, String)))

If Not IsDate(d1.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDOBirth", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDOBirth", d1.Value))
End If

If Not IsDate(d2.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDOBaptism", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDOBaptism", d2.Value))
End If

accmd.Parameters.Add(New OleDbParameter("@empQualification", CType(txt9.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empBloodGroup", CType(txt10.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empContact", CType(txt11.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empBasicSalary", CType(txt12.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empBrothers", CType(txt13.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empSisters", CType(txt14.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empAddress", CType(txt15.Text, String)))

If Not IsDate(d3.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDOJoining", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDOJoining", d3.Value))
End If

accmd.Parameters.Add(New OleDbParameter("@empPOJoining", CType(txt16.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empPresentlyStationed", CType(txt17.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empStatus", CheckBox1.Checked = True))

If Not IsDate(d4.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDOLeaving", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDOLeaving", d4.Value))
End If

If Not IsDate(d5.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDOSuspension", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDOSuspension", d5.Value))
End If

If Not IsDate(d6.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDODismissal", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDODismissal", d6.Value))
End If

If Not IsDate(d7.Text) Then
accmd.Parameters.Add(New OleDbParameter("@empDORejoining", DBNull.Value))
Else
accmd.Parameters.Add(New OleDbParameter("@empDORejoining", d7.Value))
End If

accmd.Parameters.Add(New OleDbParameter("@empRemarks", CType(txt17.Text, String)))
accmd.Parameters.Add(New OleDbParameter("@empPhoto", ConvertMyImage(PictureBox1.Image)))
Try
accmd.ExecuteNonQuery()
accmd.Dispose()
accon.Close()
MessageBox.Show("added")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


End Sub

Private Function ConvertMyImage(ByVal myImage As Image) As Byte()
Dim mstream As New MemoryStream()
myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)

Dim mbytes(mstream.Length - 1) As Byte
mstream.Position = 0
mstream.Read(mbytes, 0, mstream.Length)
Return mbytes
End Function
End Class
Posted
Updated 6-Jun-16 20:05pm

1 solution

You're missing a close bracket:
SQL
INSERT INTO [empTable] ([empCode], [batch], [empMasterCode], [empName], [empFatherName], [empMotherName], [empVillage], [empParish], [empDeanery], [empDiocese], [empDOBirth], [empDOBaptism], [empQualification], [empBloodGroup], [empContact], [empBasicSalary], [empBrothers], [empSisters], [empAddress], [empDOJoining], [empPOJoining], [empPresentlyStationed], [empStatus], [empDOLeaving], [empDOSuspension], [empDODismissal], [empDORejoining], [empRemarks], [empPhoto] values(@empCode, @batch, @empMasterCode, @empName, @empFatherName, @empMotherName, @empVillage, @empParish, @empDeanery, @empDiocese, @empDOBirth, @empDOBaptism, @empQualification, @empBloodGroup, @empContact, @empBasicSalary, @empBrothers, @empSisters, @empAddress, @empDOJoining, @empPOJoining, @empPresentlyStationed, @empStatus, @empDOLeaving, @empDOSuspension, @empDODismissal, @empDORejoining, @empRemarks, @empPhoto)
If I rip out the content, it's more obvious:
SQL
INSERT INTO [empTable] ([empCode], ...  [empRemarks], [empPhoto] values(@empCode, ... @empRemarks, @empPhoto)
Yo u need to close the list of columns with a ')':
VB.NET
Dim accmd As New OleDbCommand("INSERT INTO [empTable] ([empCode], [batch], [empMasterCode], [empName], [empFatherName], [empMotherName], [empVillage], [empParish], [empDeanery], [empDiocese], [empDOBirth], [empDOBaptism], [empQualification], [empBloodGroup], [empContact], [empBasicSalary], [empBrothers], [empSisters], [empAddress], [empDOJoining], [empPOJoining], [empPresentlyStationed], [empStatus], [empDOLeaving], [empDOSuspension], [empDODismissal], [empDORejoining], [empRemarks], [empPhoto]) values(@empCode, @batch, @empMasterCode, @empName, @empFatherName, @empMotherName, @empVillage, @empParish, @empDeanery, @empDiocese, @empDOBirth, @empDOBaptism, @empQualification, @empBloodGroup, @empContact, @empBasicSalary, @empBrothers, @empSisters, @empAddress, @empDOJoining, @empPOJoining, @empPresentlyStationed, @empStatus, @empDOLeaving, @empDOSuspension, @empDODismissal, @empDORejoining, @empRemarks, @empPhoto)", accon)
 
Share this answer
 
Comments
Rajan Pandit 7-Jun-16 3:00am    
can you please tell me.. at where I have to add the ")"... I am unable to find
OriginalGriff 7-Jun-16 4:41am    
If you look at the last lump of code, I've already added it for you...it's immediately after "[empPhoto]" and before "values"
Rajan Pandit 7-Jun-16 6:04am    
thank dear.. it is working..
OriginalGriff 7-Jun-16 6:42am    
You're welcome!

For future reference, don't call people "dear" online - I know it's an "Indian thing" but in many parts of the western world it's considered patronising or even rude, particularly if addressed to a female. Since you can't tell the gender of anyone online, it's best to avoid using it completely.

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