I am working on a VB6 application using an Access database. The application writes messages to the log table from time to time. Several instances of the application can be launched simultaneously and distinguish them, each of them has its own launch number. The start number is derived from the log table, so ...
Set record_set = New ADODB.Recordset
query_string = "SELECT MAX(RUN_NUMBER) + 1 AS NEW_RUN_NUMBER FROM ERROR_LOG"
record_set.CursorLocation = adUseClient
record_set.Open query_string, database_connection, adOpenStatic, , adCmdText
record_set.MoveLast
If IsNull(record_set.Fields("NEW_RUN_NUMBER")) Then
run_number = 0
Else
run_number = record_set.Fields("NEW_RUN_NUMBER")
End If
command_string = "INSERT INTO ERROR_LOG (RUN_NUMBER, SEVERITY, MESSAGE) " & _
" VALUES (" & Str$(run_number) & ", " & _
" " & Str$(SEVERITY_INFORMATION) & ", " & _
" 'Run Started'); "
database_connection.Execute command_string
Obviously, there is a slight gap between calculating the run number and the appearance of a new row in the database and preventing another instance from accessing between the two operations that I would like to lock the table; something along the lines
SET TRANSACTION READ WRITE RESERVING ERROR_LOG FOR PROTECTED WRITE;
How should I do it? Will a record record a record (a row in a recordset does not match any particular row in the database)?
source