ok, going more into details:
Verification column have prefilled values from 3000-9999. My goal was to create VBA macro (in Excel) which will return Verification which has not been used before, AND which is +1 value gotten previously by any user.
To do that macro searches for first free value in mID column, place new mID there, puts current date in getDate column, and returns corresponding Verification. (code below)
Macro seems to be working well, when 1 user uses that (it returnes new unique Verification every time, even if you lauch it instantly), but when 2 users run that macro in (more less) same time, they got the same Verification number.
(So let's say that user with mID1234 launches macro. Macro is able to find free record, leave mID, date, and returns corresponding Verification (let's say 3100). But when second user launches macro very fast he gets the same verification number (and overwrites data left by user mID1234).
Code:
Option Explicit
Const sDBPath AsString="\\SP_Address\Mydatabase.accdb"
Public errDescription AsString
Sub Main()
Dim ws As Worksheet
Set ws = setSheetByCodename(ThisWorkbook,"shtTestDatabase")
Dim sID AsString
sID = ws.Range("rngID")
Dim sVerificationNr AsString
sVerificationNr = updateAndGetVerificationNumber(sID)
Dim sOutputPhrase AsString
sOutputPhrase ="mID: "& sID &", VerificationNr: "& sVerificationNr
ws.Range("rngVerificationNr")= sOutputPhrase
EndSub
'main procedure - connects to Access Database in sDBPath, searches returns first free verification number (which has no value in Mid field)
Function updateAndGetVerificationNumber(sMyMID AsString)AsString
Dim ErrorOccured AsBoolean
Dim cn AsObject
Dim rs AsObject
Dim sCon AsString
Dim sSelectSQL AsString
Dim sUpateSQL AsString
ErrorOccured =True
OnErrorGoTo errQuit
sSelectSQL ="SELECT TOP 1 tblVerificationNr.VerificationNumber, tblVerificationNr.Mid, tblVerificationNr.getDate FROM tblVerificationNr WHERE (((tblVerificationNr.[mID]) = '"& sMyMID &"')) ORDER BY tblVerificationNr.getDate DESC, tblVerificationNr.ID"
sUpateSQL ="UPDATE (SELECT TOP 1 tblVerificationNr.VerificationNumber, tblVerificationNr.Mid, tblVerificationNr.getDate FROM tblVerificationNr WHERE (((IsNull([tblVerificationNr].[mID]))<>False)) ORDER BY tblVerificationNr.VerificationNumber) AS a SET a.mID = '"& sMyMID &"', getDate = '"& Now &"'"
sCon ="Provider=Microsoft.ACE.OLEDB.12.0; Data Source="& sDBPath
Set cn = CreateObject("ADODB.Connection")
cn.Open sCon
'Added the following four lines
Set rs = CreateObject("ADODB.RECORDSET")
rs.ActiveConnection = cn
cn.Execute sUpateSQL
rs.Open sSelectSQL
updateAndGetVerificationNumber = rs.Fields(0)
ErrorOccured =False
errQuit:
OnErrorResumeNext
IfNot rs IsNothingThen rs.Close
IfNot cn IsNothingThen cn.Close
Set cn =Nothing
OnErrorGoTo0
If ErrorOccured Then
updateAndGetVerificationNumber =""
errDescription ="Verification number could not be found in database. Possible reasons:"& vbCrLf & _
"- You dont' have access to Database"& vbCrLf & _
"- Database is offline,"& vbCrLf & _
"- Current Database is full."
EndIf
EndFunction