N
Nicholas Scarpinato
I've got a piece of code in a form on my front end db that updates a record
in a table based on certain criteria. My problem is that this code seems to
have a flaw in it that allows multiple instances of this code, running on
different machines, to access the exact same record in the table, if the
following conditions are met:
1. The code is executed at the exact same time
2. The record being updated in the target table has no current value
The table has three fields: Bin Number, Vendor Name, and Count. My problem
is that when Vendor Name is empty when the code is run by two users at the
exact same time, the Vendor Name will be filled with one of the vendor names
coming from the source form, but the count will be counted as two phones, one
for each of the users entering data. The second vendor name drops out of the
database completely and goes unaccounted for. This database is for returns
processing for a warehouse that runs through about $100,000-$200,000 worth of
returns per day, so these kinds of issues are exactly what we're trying to
avoid. Here's my code for the form... I hope that somebody can help me sort
out the flaw that's allowing this to happen:
Function BinLocations(VendorName)
DoCmd.SetWarnings False
Dim db, rs, rs2, sql, sql2, FormText, BinLoc
Dim VendorBinCode
Set db = CurrentDb()
sql = "SELECT * FROM [Bin Locations and Counts Table];"
sql2 = "SELECT * FROM [Main Returns Table];"
Set rs = db.OpenRecordset(sql)
Set rs2 = db.OpenRecordset(sql2)
With rs
.MoveFirst
Do Until .EOF
VendorBinCode = rs.Fields("Vendor Name")
If VendorName = VendorBinCode Then GoTo FoundBin
.MoveNext
Loop
GoTo NewBin
FoundBin:
BinLoc = rs.Fields("Bin Location")
FormText = "Bin location found!" & vbNewLine & "Please use bin " &
BinLoc & "."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, FormText
.Edit
rs.Fields("Count") = rs.Fields("Count") + 1
.Update
With rs2
.MoveFirst
Do Until .EOF
If rs2.Fields("SIM/ESN/IMEI") <> Forms![Returns Entry
Form].[SIM/ESN/IMEI] Then GoTo SkipLoop
rs2.Edit
rs2.Fields("Long Status Description") = BinLoc & " - " &
VendorName
rs2.Update
SkipLoop:
.MoveNext
Loop
End With
GoTo LocationEnd
NewBin:
rs.MoveFirst
Do Until .EOF
If rs.Fields("Vendor Name") = 0 Then GoTo MakeNewLocation
.MoveNext
Loop
GoTo LocationError
MakeNewLocation:
BinLoc = rs.Fields("Bin Location")
rs.Edit
rs.Fields("Vendor Name") = VendorName
rs.Fields("Count") = rs.Fields("Count") + 1
rs.Update
With rs2
.MoveFirst
Do Until .EOF
If rs2.Fields("SIM/ESN/IMEI") <> Forms![Returns Entry
Form].[SIM/ESN/IMEI] Then GoTo SkipLoop2
rs2.Edit
rs2.Fields("Long Status Description") = BinLoc & " - " &
VendorName
rs2.Update
FormText = "Bin location not found! New bin location created.
Please use bin " & BinLoc & "."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, FormText
SkipLoop2:
.MoveNext
Loop
End With
GoTo LocationEnd
LocationError:
FormText = "All locations appear to be in use! Please clear a location
before continuing."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, FormText
GoTo LocationEnd
LocationEnd:
End With
rs.Close
rs2.Close
db.Close
DoCmd.SetWarnings True
End Function
in a table based on certain criteria. My problem is that this code seems to
have a flaw in it that allows multiple instances of this code, running on
different machines, to access the exact same record in the table, if the
following conditions are met:
1. The code is executed at the exact same time
2. The record being updated in the target table has no current value
The table has three fields: Bin Number, Vendor Name, and Count. My problem
is that when Vendor Name is empty when the code is run by two users at the
exact same time, the Vendor Name will be filled with one of the vendor names
coming from the source form, but the count will be counted as two phones, one
for each of the users entering data. The second vendor name drops out of the
database completely and goes unaccounted for. This database is for returns
processing for a warehouse that runs through about $100,000-$200,000 worth of
returns per day, so these kinds of issues are exactly what we're trying to
avoid. Here's my code for the form... I hope that somebody can help me sort
out the flaw that's allowing this to happen:
Function BinLocations(VendorName)
DoCmd.SetWarnings False
Dim db, rs, rs2, sql, sql2, FormText, BinLoc
Dim VendorBinCode
Set db = CurrentDb()
sql = "SELECT * FROM [Bin Locations and Counts Table];"
sql2 = "SELECT * FROM [Main Returns Table];"
Set rs = db.OpenRecordset(sql)
Set rs2 = db.OpenRecordset(sql2)
With rs
.MoveFirst
Do Until .EOF
VendorBinCode = rs.Fields("Vendor Name")
If VendorName = VendorBinCode Then GoTo FoundBin
.MoveNext
Loop
GoTo NewBin
FoundBin:
BinLoc = rs.Fields("Bin Location")
FormText = "Bin location found!" & vbNewLine & "Please use bin " &
BinLoc & "."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, FormText
.Edit
rs.Fields("Count") = rs.Fields("Count") + 1
.Update
With rs2
.MoveFirst
Do Until .EOF
If rs2.Fields("SIM/ESN/IMEI") <> Forms![Returns Entry
Form].[SIM/ESN/IMEI] Then GoTo SkipLoop
rs2.Edit
rs2.Fields("Long Status Description") = BinLoc & " - " &
VendorName
rs2.Update
SkipLoop:
.MoveNext
Loop
End With
GoTo LocationEnd
NewBin:
rs.MoveFirst
Do Until .EOF
If rs.Fields("Vendor Name") = 0 Then GoTo MakeNewLocation
.MoveNext
Loop
GoTo LocationError
MakeNewLocation:
BinLoc = rs.Fields("Bin Location")
rs.Edit
rs.Fields("Vendor Name") = VendorName
rs.Fields("Count") = rs.Fields("Count") + 1
rs.Update
With rs2
.MoveFirst
Do Until .EOF
If rs2.Fields("SIM/ESN/IMEI") <> Forms![Returns Entry
Form].[SIM/ESN/IMEI] Then GoTo SkipLoop2
rs2.Edit
rs2.Fields("Long Status Description") = BinLoc & " - " &
VendorName
rs2.Update
FormText = "Bin location not found! New bin location created.
Please use bin " & BinLoc & "."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, FormText
SkipLoop2:
.MoveNext
Loop
End With
GoTo LocationEnd
LocationError:
FormText = "All locations appear to be in use! Please clear a location
before continuing."
DoCmd.OpenForm "Bin Confirmation Form", , , , , acDialog, FormText
GoTo LocationEnd
LocationEnd:
End With
rs.Close
rs2.Close
db.Close
DoCmd.SetWarnings True
End Function