PDA

View Full Version : Error in Code in program using vb.net and Ms Access


Cathy30
04-07-2011, 08:17 AM
Hi

iam working with vb.net and ms access i have a error in the below coding


Private Sub Save_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save_Button.Click
If Code_TextBox.Text <> "" And Name_TextBox.Text <> "" Then
'Dim conn As New SqlConnection(connstr)
Dim code, Name As String
code = Code_TextBox.Text
Name = Name_TextBox.Text
str = "insert into Table1 values(" & code & "," & Name & ")"
cn.Open()
cmd = New OleDbCommand(str, cn)
cmd.ExecuteNonQuery() -------> The error is "No value given for one or more required parameters."
cn.Close()
Clear()
MsgBox("Records Saved")
Else
MsgBox("Fields cannot be empty")
End If

End Sub

Help plz

guelphdad
04-07-2011, 03:11 PM
You are posting in a forum to do with MySQL. I'll move your thread to the general database forum, though you are likely best to find an Access forum to ask your question.

Old Pedant
04-11-2011, 09:27 PM
str = "insert into Table1 values(" & code & "," & Name & ")"

That query would only be legal if *BOTH* of the following applied:

(a) Your TABLE1 has only two fields in it.
(b) Those two fields are *BOTH* numeric fields. That is, they will never hold text values, only numeric data.

[Well, not quite true. You *could* have three fields in the table *if* one of the fields is an autoincrement field.]

ANYWAY...

*PROBABLY* your query needs to look like this:

str = "insert into Table1 (field1, field2) values(" & code & ",'" & Name & "')"

where you replace field1 and field2 with the actual names of the fields.

And if field1 (the field that will receive the code value) is NOT numeric, then you would need apostrophes around code as well, thus:

str = "insert into Table1 (field1, field2) values('" & code & "','" & Name & "')"


Though this would work, it is *NOT* the best way to create and use queries in VB.NET and ADO.NET. You really need to go look at how to create parameterized queries with the OleDbCommand object and start using them.