tjdrake
New Member
- Joined
- Aug 2, 2022
- Messages
- 19
- Office Version
- 2021
- Platform
- Windows
Hello Team,
I've been struggling with this all week, and am running up against a deadline to make this work for my team. Here are the essentials:
Sub updateAccess(blnEndofShift As Boolean)
If Not blnEndofShift Then Exit Sub
'Presentation Management
Call ToggleHideUnhideDataSheets("Show")
'Resume Next on an Error
Dim strErrMsg As String
On Error GoTo ErrorMessage
Let strErrMsg = ""
'Connection Declarations
Let strErrMsg = "Connection"
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\...\Database\RTS_SPT_Data.accdb;Persist Security Info=False;"
cn.Open
On Error Resume Next
Dim tmTimeKeeper As Date
If Err.Number = -2147467259 Then tmTimeKeeper = Now + TimeValue("00:01:00")
Do While cn.State <> adStateOpen And Now <= tmTimeKeeper
cn.Open
DoEvents
Application.Wait (Now + TimeValue("0:00:5"))
If cn.State = adStateOpen Then
Err.Clear
Exit Do
End If
Loop
If Not cn.State = adStateOpen Then
MsgBox "Please try again. The DB server is locked at this time", vbOKCancel, , "DB Write Error"
GoTo ExitSub
End If
'Turn-off Resume Next
On Error GoTo ErrorMessage
'Recordset Declaration
Dim rstADO As ADODB.Recordset
Set rstADO = New ADODB.Recordset
Let strErrMsg = "Opening DB"
rstADO.Open "RTS_SPT_Adoption_Data", cn, adOpenDynamic, adLockOptimistic, adCmdTable
' Types of Locks - ActiveX Data Objects (ADO)
'adLockBatchOptimistic
'adLockOptimistic
'adLockPessimistic
'adLockReadOnly
'adLockUnspecified
'Add a new record
Let strErrMsg = "Adding a new record."
rstADO.AddNew
'Declarations
Dim wbWorkbook As Workbook
Dim wsWorksheet As Worksheet
Dim rngName As Range
Dim intFind1 As Integer
Dim intFind2 As Integer
Dim strFound As String
Dim sProduct As String
Dim cPrice As String
Dim intCntr As Integer
Dim dynArrayStr() As String
Dim intArrayMax As Integer
Const cnstFindStr1 As String = "RTS"
Const cnstFindStr2 As String = "Total"
Set wbWorkbook = ThisWorkbook
Set wsWorksheet = wbWorkbook.Sheets("parms")
' Load Station, DateTime Stamp, DS Reset Owner
Let strErrMsg = "Updating fields in a record."
Set wsWorksheet = wbWorkbook.Sheets("parms")
rstADO!DeliveryStation = wsWorksheet.Range("DlvryStn")
rstADO!DateTime = Now
rstADO!Shift = "RTS"
rstADO!StowBagReplenOwner = wsWorksheet.Range("StowBagReplenOwner")
rstADO!DCAPInductOVPkgPct = wsWorksheet.Range("DCAP_Induct_OV_package")
rstADO!AvgShipPerBag = wsWorksheet.Range("Avg_Shipments_per_Bag")
' Other Plan Parameters
rstADO!StowBagReplenOwner = wsWorksheet.Range("StowBagReplenOwner")
' Load Core and PFSD Volume
Set wsWorksheet = wbWorkbook.Sheets("PvA")
rstADO!CoreVolumePlan = wsWorksheet.Range("E5")
rstADO!CoreVolumeActual = wsWorksheet.Range("F5")
rstADO!DispatchVolumePlan = wsWorksheet.Range("J5")
rstADO!DispatchVolumeActual = wsWorksheet.Range("K5")
rstADO!AdhocVolumePlan = wsWorksheet.Range("O5")
rstADO!AdhocVolumeActual = wsWorksheet.Range("P5")
rstADO!SWAVolumePlan = 0
rstADO!SWAVolumeActual = 0
rstADO!SameDayVolumePlan = wsWorksheet.Range("T5")
rstADO!SameDayVolumeActual = wsWorksheet.Range("U5")
rstADO!RTSVolumePlan = wsWorksheet.Range("Y5")
rstADO!RTSVolumeActual = wsWorksheet.Range("Z5")
rstADO!TotalVolumePlan = wsWorksheet.Range("AD5")
rstADO!TotalVolumeActual = wsWorksheet.Range("AE5")
rstADO!HistBagsLeftover = IIf(IsNumeric(wsWorksheet.Range("HistLeftoverBags")), wsWorksheet.Range("HistLeftoverBags"), 0)
rstADO!HistAdhocVolume = IIf(IsNumeric(wsWorksheet.Range("HistAdhocVolume")), wsWorksheet.Range("HistAdhocVolume"), 0)
'Find the columns that have the values sought
wsWorksheet.Rows(3).Select
Let strFound = Selection.Find(cnstFindStr1, lookat:=xlWhole, MatchCase:=False).AddressLocal
Let intFind1 = Range(strFound).Column
Let strFound = Selection.Find(cnstFindStr2, lookat:=xlWhole, MatchCase:=False).AddressLocal
Let intFind2 = Range(strFound).Column
'Begin preparation for processing the day's plan and actuals (PvA Tab)
Let intArrayMax = wsWorksheet.Range("PvA_ProcessPaths").Count
ReDim dynArrayStr(intArrayMax)
For intCntr = 1 To intArrayMax
wsWorksheet.Range("PvA_ProcessPaths").Activate
If Selection(intCntr) <> "" Then
Let dynArrayStr(intCntr) = Selection(intCntr)
End If
Next
' Load Plan Rates and Hours (Plan and Actual)
Let intCntr = 0
For intCntr = 1 To intArrayMax
DoEvents
Select Case Selection(intCntr)
Case "Auto Scan Induct Loader"
rstADO!AutoScanInductLoader_PlanRate = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind2 - 1)), Selection(intCntr).Offset(0, intFind2 - 1), 0)
rstADO!RTSAutoScanInductLoader_PlanHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind1 - 2)), Selection(intCntr).Offset(0, intFind1 - 2), 0)
rstADO!RTSAutoScanInductLoader_ActualHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind1 - 1)), Selection(intCntr).Offset(0, intFind1 - 1), 0)
rstADO!TotalAutoScanInductLoader_PlanHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind2 - 2)), Selection(intCntr).Offset(0, intFind2 - 2), 0)
rstADO!TotalAutoScanInductLoader_ActualHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind2 - 1)), Selection(intCntr).Offset(0, intFind2 - 1), 0)
.
.
.
Case ""
'Dummy Case
Case Else
MsgBox Selection(cntrarray) & "Not Found When Uploading the EOS Plan", vbOKOnly
End Select
DoEvents
Next
'Update the record
Let strErrMsg = "Performing record update."
rstADO.Update
DoEvents
ExitSub:
' Perform Clean-up
Let strErrMsg = "Closing the DB."
rstADO.Close
Set rstADO = Nothing
DoEvents
cn.Close
Set cn = Nothing
DoEvents
'Presentation Management
Range("SameDayBagResetOwner").Activate
Call ToggleHideUnhideDataSheets("Hide")
MsgBox "End of Shift Upload Complete.", vbOKOnly, "RTS SPT Update"
If Err.Number = 0 Then Exit Sub
ErrorMessage:
Let strErrMsg = "An Error Occurred. Error: " & Err.Number & " - Descr: " & Err.Description & " - " & strErrMsg
MsgBox strErrMsg, vbOKOnly, "Error Handler"
On Error Resume Next
' Perform Clean-up
Set rstADO = Nothing
Set cn = Nothing
'Presentation Management
Call ToggleHideUnhideDataSheets("Hide")
End Sub
I've been struggling with this all week, and am running up against a deadline to make this work for my team. Here are the essentials:
- The MS 2021 Access DB sits on a SharePoint Server;
- The SharePoint permissions have been granted to Everyone;
- The code updates with a new record perfectly every time from my desktop;
- When other users attempt to update the DB, the response is an error code 3709, "The connection cannot be used to perform this operation. It is either closed or invalid in this context."
- I've adLockOptimistic and adLockPessimistic without any yield;
- I believe that it has to do with MS Access creating the record lock file, laccdb, but I am no MS Access SME, so hear I am...
- Client Settings: Default Open Mode:=Shared; Default Record Locking:=No Locks.
Sub updateAccess(blnEndofShift As Boolean)
If Not blnEndofShift Then Exit Sub
'Presentation Management
Call ToggleHideUnhideDataSheets("Show")
'Resume Next on an Error
Dim strErrMsg As String
On Error GoTo ErrorMessage
Let strErrMsg = ""
'Connection Declarations
Let strErrMsg = "Connection"
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\...\Database\RTS_SPT_Data.accdb;Persist Security Info=False;"
cn.Open
On Error Resume Next
Dim tmTimeKeeper As Date
If Err.Number = -2147467259 Then tmTimeKeeper = Now + TimeValue("00:01:00")
Do While cn.State <> adStateOpen And Now <= tmTimeKeeper
cn.Open
DoEvents
Application.Wait (Now + TimeValue("0:00:5"))
If cn.State = adStateOpen Then
Err.Clear
Exit Do
End If
Loop
If Not cn.State = adStateOpen Then
MsgBox "Please try again. The DB server is locked at this time", vbOKCancel, , "DB Write Error"
GoTo ExitSub
End If
'Turn-off Resume Next
On Error GoTo ErrorMessage
'Recordset Declaration
Dim rstADO As ADODB.Recordset
Set rstADO = New ADODB.Recordset
Let strErrMsg = "Opening DB"
rstADO.Open "RTS_SPT_Adoption_Data", cn, adOpenDynamic, adLockOptimistic, adCmdTable
' Types of Locks - ActiveX Data Objects (ADO)
'adLockBatchOptimistic
'adLockOptimistic
'adLockPessimistic
'adLockReadOnly
'adLockUnspecified
'Add a new record
Let strErrMsg = "Adding a new record."
rstADO.AddNew
'Declarations
Dim wbWorkbook As Workbook
Dim wsWorksheet As Worksheet
Dim rngName As Range
Dim intFind1 As Integer
Dim intFind2 As Integer
Dim strFound As String
Dim sProduct As String
Dim cPrice As String
Dim intCntr As Integer
Dim dynArrayStr() As String
Dim intArrayMax As Integer
Const cnstFindStr1 As String = "RTS"
Const cnstFindStr2 As String = "Total"
Set wbWorkbook = ThisWorkbook
Set wsWorksheet = wbWorkbook.Sheets("parms")
' Load Station, DateTime Stamp, DS Reset Owner
Let strErrMsg = "Updating fields in a record."
Set wsWorksheet = wbWorkbook.Sheets("parms")
rstADO!DeliveryStation = wsWorksheet.Range("DlvryStn")
rstADO!DateTime = Now
rstADO!Shift = "RTS"
rstADO!StowBagReplenOwner = wsWorksheet.Range("StowBagReplenOwner")
rstADO!DCAPInductOVPkgPct = wsWorksheet.Range("DCAP_Induct_OV_package")
rstADO!AvgShipPerBag = wsWorksheet.Range("Avg_Shipments_per_Bag")
' Other Plan Parameters
rstADO!StowBagReplenOwner = wsWorksheet.Range("StowBagReplenOwner")
' Load Core and PFSD Volume
Set wsWorksheet = wbWorkbook.Sheets("PvA")
rstADO!CoreVolumePlan = wsWorksheet.Range("E5")
rstADO!CoreVolumeActual = wsWorksheet.Range("F5")
rstADO!DispatchVolumePlan = wsWorksheet.Range("J5")
rstADO!DispatchVolumeActual = wsWorksheet.Range("K5")
rstADO!AdhocVolumePlan = wsWorksheet.Range("O5")
rstADO!AdhocVolumeActual = wsWorksheet.Range("P5")
rstADO!SWAVolumePlan = 0
rstADO!SWAVolumeActual = 0
rstADO!SameDayVolumePlan = wsWorksheet.Range("T5")
rstADO!SameDayVolumeActual = wsWorksheet.Range("U5")
rstADO!RTSVolumePlan = wsWorksheet.Range("Y5")
rstADO!RTSVolumeActual = wsWorksheet.Range("Z5")
rstADO!TotalVolumePlan = wsWorksheet.Range("AD5")
rstADO!TotalVolumeActual = wsWorksheet.Range("AE5")
rstADO!HistBagsLeftover = IIf(IsNumeric(wsWorksheet.Range("HistLeftoverBags")), wsWorksheet.Range("HistLeftoverBags"), 0)
rstADO!HistAdhocVolume = IIf(IsNumeric(wsWorksheet.Range("HistAdhocVolume")), wsWorksheet.Range("HistAdhocVolume"), 0)
'Find the columns that have the values sought
wsWorksheet.Rows(3).Select
Let strFound = Selection.Find(cnstFindStr1, lookat:=xlWhole, MatchCase:=False).AddressLocal
Let intFind1 = Range(strFound).Column
Let strFound = Selection.Find(cnstFindStr2, lookat:=xlWhole, MatchCase:=False).AddressLocal
Let intFind2 = Range(strFound).Column
'Begin preparation for processing the day's plan and actuals (PvA Tab)
Let intArrayMax = wsWorksheet.Range("PvA_ProcessPaths").Count
ReDim dynArrayStr(intArrayMax)
For intCntr = 1 To intArrayMax
wsWorksheet.Range("PvA_ProcessPaths").Activate
If Selection(intCntr) <> "" Then
Let dynArrayStr(intCntr) = Selection(intCntr)
End If
Next
' Load Plan Rates and Hours (Plan and Actual)
Let intCntr = 0
For intCntr = 1 To intArrayMax
DoEvents
Select Case Selection(intCntr)
Case "Auto Scan Induct Loader"
rstADO!AutoScanInductLoader_PlanRate = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind2 - 1)), Selection(intCntr).Offset(0, intFind2 - 1), 0)
rstADO!RTSAutoScanInductLoader_PlanHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind1 - 2)), Selection(intCntr).Offset(0, intFind1 - 2), 0)
rstADO!RTSAutoScanInductLoader_ActualHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind1 - 1)), Selection(intCntr).Offset(0, intFind1 - 1), 0)
rstADO!TotalAutoScanInductLoader_PlanHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind2 - 2)), Selection(intCntr).Offset(0, intFind2 - 2), 0)
rstADO!TotalAutoScanInductLoader_ActualHours = IIf(IsNumeric(Selection(intCntr).Offset(0, intFind2 - 1)), Selection(intCntr).Offset(0, intFind2 - 1), 0)
.
.
.
Case ""
'Dummy Case
Case Else
MsgBox Selection(cntrarray) & "Not Found When Uploading the EOS Plan", vbOKOnly
End Select
DoEvents
Next
'Update the record
Let strErrMsg = "Performing record update."
rstADO.Update
DoEvents
ExitSub:
' Perform Clean-up
Let strErrMsg = "Closing the DB."
rstADO.Close
Set rstADO = Nothing
DoEvents
cn.Close
Set cn = Nothing
DoEvents
'Presentation Management
Range("SameDayBagResetOwner").Activate
Call ToggleHideUnhideDataSheets("Hide")
MsgBox "End of Shift Upload Complete.", vbOKOnly, "RTS SPT Update"
If Err.Number = 0 Then Exit Sub
ErrorMessage:
Let strErrMsg = "An Error Occurred. Error: " & Err.Number & " - Descr: " & Err.Description & " - " & strErrMsg
MsgBox strErrMsg, vbOKOnly, "Error Handler"
On Error Resume Next
' Perform Clean-up
Set rstADO = Nothing
Set cn = Nothing
'Presentation Management
Call ToggleHideUnhideDataSheets("Hide")
End Sub