My application manages construction projects, which can change many times before product is ordered. My goal is to create an audit trail of changes so that they can be verified or explained to the customer, and so that mistakes and malfiance can be caught and corrected. save a separate copy of the current record in memory. When the record is saved, I want to compare it to the copy in memory for changes. If any, I want to create a record in the same table with the original data. The DateChanged field will get the current date/time (which remains null for the original record). There is also a User_ID field to document who created this record.
My problem is figuring out how to manipulate data that is not on the user's form. I suppose that I can create invisible text controls, but that's rather clunky. I'm sure that there are ways to manipulate the data directly in the DataTable, but I've found no decipherable and workable example so far. What I have found is either for VB6 (which I know but I no longer have the compiler/IDE, it won't run easily on Windows 10 and is unsupported as well) or depends on some parameters outside the VB code they show ... please note that I specified the parameters I used. Idealy I'd directly read from and write to the DataTable without involving the form at all.
The example code below is from a book, which is based on VB 2019, hence the reason I'm not using VB 2022.
Table structure:
Author - Short Text, primary key
Year_Born - Integer
User_ID - Short Text
DateChanged - Date/Time
' Visual Basic 2019. The database was created in Access 2019.
' Target CPU has been set to x64.
' In Manage NuGet I have added system.data.oledb, version 6.00.
' I added the reference for "Microsoft ActiveX Data Objects 6.1 Library"
Imports System.Data
Imports System.Data.OleDb
Public Class frmAuthor
Dim BooksConnection As OleDbConnection
Dim AuthorsCommand As OleDbCommand
Dim AuthorsAdapter As OleDbDataAdapter
Dim AuthorsTable As DataTable
Dim AuthorsManager As CurrencyManager ' Currency as in "Keeping current", not money
Dim MyState As String = "View" ' for noticing state transitions
Dim MyBookmark As Integer ' for cancelling adds
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BooksConnection = New OleDbConnection ' (ConnectionString)
BooksConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; " _
+ "Data Source = C:\VBDB\BooksDb.accdb"
BooksConnection.Open()
AuthorsCommand = New OleDbCommand("SELECT * from Authors ORDER BY Author", BooksConnection)
AuthorsAdapter = New OleDbDataAdapter()
AuthorsAdapter.SelectCommand = AuthorsCommand
AuthorsTable = New DataTable()
AuthorsAdapter.Fill(AuthorsTable)
txtAuthorName.DataBindings.Add("Text", AuthorsTable, "Author")
txtYearBorn.DataBindings.Add("Text", AuthorsTable, "Year_Born")
AuthorsManager = DirectCast(Me.BindingContext(AuthorsTable), CurrencyManager)
Me.Show()
Call SetState("View")
End Sub
End Case