I am trying to update a row in a database that already holds data but instead, my code just creates a new row each time. Does the SELECT statement determine what will update? This code does not run but if I replace the SELECT statement with SELECT *, it will update the database but only create a new row with the updates.
All I am looking to do is to update one field name OutTime in a row
WHERE EmpNum = globalEmpNum AND TodayD = Today.
TodayD is of type date in the database.
Thanks for any help!
All I am looking to do is to update one field name OutTime in a row
WHERE EmpNum = globalEmpNum AND TodayD = Today.
TodayD is of type date in the database.
Thanks for any help!
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] globalDeptNum = 4500 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Sales Department[/COLOR][/SIZE]
[SIZE=2]conn = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0; data source=c:\PR\Payroll.mdb")[/SIZE]
[SIZE=2]Dim[SIZE=2] sql [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "SELECT * FROM Hours WHERE [EmpNum] = '"[/SIZE]
[SIZE=2]sql = sql & globalEmpNum[/SIZE]
[SIZE=2]sql = sql & "' AND [TodayD] = '#"[/SIZE]
[SIZE=2]sql = sql & Today()[/SIZE]
[SIZE=2]sql = sql & "#'"[/SIZE]
[/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]da = New OleDbDataAdapter(sql, conn)[/COLOR][/SIZE]
[SIZE=2]myDataTable = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable[/SIZE]
[SIZE=2]da.Fill(myDataTable)[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]Console.WriteLine("Error Opening {0}", conn.DataSource)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]myCommand = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OleDbCommandBuilder(da)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rowArticle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow = myDataTable.NewRow()[/SIZE]
[SIZE=2]rowArticle("OutTime") = TimeOfDay()[/SIZE]
[SIZE=2]myDataTable.Rows.Add(rowArticle)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyUpdateCommand [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OleDbCommand[/SIZE]
[SIZE=2]MyUpdateCommand = myCommand.GetUpdateCommand[/SIZE]
[SIZE=2]da.UpdateCommand = MyUpdateCommand[/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2]da.Update(myDataTable)[/SIZE]
[SIZE=2]MsgBox("Successfully punched OUT at " & TimeOfDay())[/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] Ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]MessageBox.Show(Ex.Message)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
Last edited by a moderator: