An error occurred on the INSERT INTO statement.

micheal

Member
Joined
Nov 8, 2011
Messages
6
Location
Malaysia
Programming Experience
Beginner
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If str = "add" Then
''''''ADD NEW RECORD'''''''
If txtName.Text = "" Or txtICNumber.Text = "" Or txtGender.Text = "" Or txtTelNum.Text = "" Or txtUserName.Text = "" Or txtpassword.Text = "" Then
MessageBox.Show("All fields Are Required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
myqry = "INSERT INTO Staff(StaffName,Gender,IcNumber,TelNumber,Username,Password) "
myqry = myqry + "VALUES('" & txtName.Text & "','" & txtGender.Text & "','" & txtICNumber.Text & "','" & txtTelNum.Text & _
"','" & txtUserName.Text & "','" & txtpassword.Text & "')"
mycmd = New OleDbCommand
With mycmd
.CommandText = myqry
.Connection = conn
.ExecuteNonQuery()
End With
Call Set1()
End If
Else
:crushed:
 
Given the way that you're writing your code, there could be more issues than just this but the obvious one is that you have a column named "Password" which is a reserved word in Jet SQL. It's generally best to avoid using reserved words if possible but otherwise you can escape your identifiers, which, for Access, involves wrapping them in brackets [].

You should also never use string concatenation to insert values into SQL code like that. To learn why and how to use parameters, follow the Blog link in my signature and check out my post on ADO.NET Parameters.

Also, you should pretty much always use AndAlso and OrElse in preference to And and Or. Finally, there is almost never a need to use the Call keyword in VB.NET.
 
Back
Top