As suggested, your submitExpenses sub should be wrapped in a Try/Catch block:
Notice how I use the Using keyword for the OleDbCommand, this is because OleDbCommand implements IDisposable. Another thing to notice is how I'm adding the Day parameter, if all you want to add is the Date and not the Date and Time then you need to first specify the OleDbType and then set the Value to just the Date otherwise it will return a mismatch error.Code:Sub submitExpenses() Try 'Open the connection connection.Open() 'Create a new command Using cmd As OleDbCommand = New OleDbCommand("INSERT INTO [Expenses] ([Day], [Gas], [Food], [carWash], [Vacuum], [Parts], [Misc], [Total]) VALUES (@day, @gas, @food, @carWash, @vacuum, @parts, @misc, @total);") 'Parameterize the query With cmd.Parameters .Add("@day", OleDbType.Date).Value = expense.jobDate.Date .AddWithValue("@gas", expenses.gas) .AddWithValue("@food", expenses.food) .AddWithValue("@carWash", expenses.carWash) .AddWithValue("@vacuum", expenses.vacuum) .AddWithValue("@parts", expenses.parts) .AddWithValue("@misc", expenses.misc) .AddWithValue("@total", expenses.expenseTotal) End With 'Execute the query cmd.ExecuteNonQuery() 'Close the connection connection.Close() End Using Catch ex As Exception 'Display the error Console.WriteLine(ex.ToString()) Finally 'Close the connection if it was left open If connection IsNot Nothing AndAlso connection.State = ConnectionState.Open Then connection.Close() End If End Try End Sub




Reply With Quote
