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")
acsconn.Open()
If acsconn.State = ConnectionState.Open Then
MsgBox("Connected")

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
objcmd.ExecuteNonQuery()
objcmd.Dispose()
MsgBox("Saved")

End If
acsconn.Close()

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

Recommended Answers

All 4 Replies

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?..
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.