1

I'm hoping for someone to be able to offer me some help please? I have spent the last 4 hours trying to fix this problem but not got anywhere. I dont actually have an error code which is making it even more difficult, it just doesnt do anything.

What I am trying to do is Read the value in Textbox2 and minus it from the GamesPlayed field in Table1 where the JobID matches TextBox1.

I dont see what I have done wrong? Many thanks.

 Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\Database1.mdb")
        Dim cmd As New OleDbCommand
        With cmd
            .CommandType = CommandType.Text
            .Connection = conn
            .CommandText = "UPDATE [Table1] SET GamesPlayed = GamesPlayed - " & Val(TextBox2.Text) & " WHERE JobID = TextBox1.text"
            .Parameters.Add("@p1", Me.ComboBox1.SelectedValue)
        End With
        conn.Open()
        cmd.ExecuteNonQuery()
JackSparrow
  • 389
  • 2
  • 8
  • 18

3 Answers3

2

You where off to a good start, but you need to use the parameters you add.

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\Database1.mdb")
    Dim cmd As New OleDbCommand
    With cmd
        .CommandType = CommandType.Text
        .Connection = conn
        .CommandText = "UPDATE [Table1] SET GamesPlayed = @p1 WHERE JobID = @p2"
        .Parameters.Add("@p1", Me.ComboBox1.SelectedValue)
        .Parameters.Add("@p2", Me.Textbox1.Text)
    End With
    conn.Open()
    cmd.ExecuteNonQuery()

I would also recommend naming your controls something useful and not the default designer names.

OneFineDay
  • 9,004
  • 3
  • 26
  • 37
0

Change this:

.CommandText = "UPDATE ...  JobID = TextBox1.text"

to that:

.CommandText = "UPDATE ... JobID = " & TextBox1.text
David Sdot
  • 2,343
  • 1
  • 20
  • 26
0

cmd.ExecuteNonQuery() will not directly return a value. If you would like to get a value, try cmd.ExecuteReader(). Example:

        Dim cmd As New OleDb ("UPDATE [Table1] SETCommandGamesPlayed = GamesPlayed - " & Val(TextBox2.Text) & " WHERE JobID = TextBox1.text")
        Dim yourvalvue As OleDbDataReader = cmd.ExecuteReader()

       Do While (yourvalue.read)
         'what you want to do
       Loop

This question is a good example of what the different execute functions do.

Community
  • 1
  • 1
Chase Ernst
  • 1,147
  • 1
  • 21
  • 53