Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error in Code in program using vb.net and Ms Access

    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

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Code:
    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:
    Code:
    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:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •