Hi, When i run the codes i donot get an error, howveer after clicking INsert i get the Syntax error in Insert Into statement error.
I do recieve the COnnected MsgBox which then results in the error
My code..

Sub connect()

Dim acsconn As OleDbConnection = New OleDbConnection _
("Provider=Microsoft.jet.oledb.4.0;data source=C:\Temp\Imt\Imt\Imt\Imt_db.mdb")
If acsconn.State = ConnectionState.Open Then

Dim strsql As String = "Insert into tbl_xUser (Username,Password,Department_Code) values (@field2,@field3,@field4)"

Dim objcmd As New System.Data.OleDb.OleDbCommand(strsql, acsconn) ' the oledbcommand

With objcmd
.Parameters.AddWithValue("@field2", txt_user.Text)
.Parameters.AddWithValue("@field3", txt_pass.Text)
.Parameters.AddWithValue("@field4", txt_dept.Text)

End With

End If

Please advice if you know the error :)
Thanks in advance,

Run your code in debug, get the value of strsql and paste it into a query in Access,run the query and see what it tells you.

Maybe your "Department_Code" is not a varchar type in the dataTable. If its an integer you have to convert the textBox text property to a number (int.Parse, or Convert.Toint32() methods).

Otherwise code looks clean.
Or define parameters a bit differently:

parameters.Add("@field2", SqlDbType.VarChar, 50).Value = txt_user.Text

thanks for your responce guys, i just realised that password needed [ ] which has worked :)..
However I am having a small problem with refreshing my dataGrid once i insert data. ANy idea's on how i can refresh the data?..

This article has been dead for over six months. Start a new discussion instead.