Thanks for that. This is what my final code looks like, everything works as i want it. I'd be grateful if you could cast your eyes over it to ensure I've done it correctly and perhaps suggest improvements if required.
Code:
Option Compare Database
Option Explicit
Public binType As String
Public requiredBinWidthByProduct As Long
Public requiredBinWidthByBinType As Integer
Public updateBinWidthMethod As String
Private Sub Form_Current()
setBinWidthMmTextBoxValues
End Sub
Private Sub setBinWidthMmTextBoxValues()
If Not IsNull([masterBinSelectBox]) Then
binType = Nz([masterBinSelectBox].[Column](3), 0)
requiredBinWidthByProduct = Nz(IIf(Int([rows]) = [rows], [widthMM] * [rows], ([widthMM] * Int([rows])) + [depthMM]), 0)
requiredBinWidthByBinType = Nz(DLookup("[width_mm]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 0)
updateBinWidthMethod = Nz(DLookup("[calculate_bin_by_product]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 1)
If updateBinWidthMethod = -1 Then
Me.masterBinWidthMM.Value = requiredBinWidthByProduct
ElseIf updateBinWidthMethod = 0 Then
Me.masterBinWidthMM.Value = requiredBinWidthByBinType
End If
If Not IsNull([secondaryBinSelectBox]) Then
binType = Nz([secondaryBinSelectBox].[Column](3), 0)
requiredBinWidthByBinType = Nz(DLookup("[width_mm]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 0)
updateBinWidthMethod = Nz(DLookup("[calculate_bin_by_product]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 1)
If updateBinWidthMethod = -1 Then
Me.secondaryBinWidthMM.Value = requiredBinWidthByProduct
ElseIf updateBinWidthMethod = 0 Then
Me.secondaryBinWidthMM.Value = requiredBinWidthByBinType
End If
Else
Me.secondaryBinWidthMM.Value = ""
End If
Else
Me.masterBinWidthMM.Value = ""
Me.secondaryBinWidthMM.Value = ""
End If
End Sub
Private Sub recalculateBinSize()
If Not IsNull([masterBinSelectBox]) Then
Dim binID As Integer
Dim updateBinWidthByProduct As String
Dim updateBinWidthByBinType As String
binID = Nz([masterBinSelectBox].[Column](1), 0)
binType = Nz([masterBinSelectBox].[Column](3), 0)
requiredBinWidthByProduct = Nz(IIf(Int([rows]) = [rows], [widthMM] * [rows], ([widthMM] * Int([rows])) + [depthMM]), 0)
requiredBinWidthByBinType = Nz(DLookup("[width_mm]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 0)
updateBinWidthMethod = Nz(DLookup("[calculate_bin_by_product]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 1)
updateBinWidthByProduct = "UPDATE tblBin SET [bin_width_mm] ='" & (requiredBinWidthByProduct) & "' WHERE [bin_id] = " & (binID)
updateBinWidthByBinType = "UPDATE tblBin SET [bin_width_mm] ='" & (requiredBinWidthByBinType) & "' WHERE [bin_id] = " & (binID)
If updateBinWidthMethod = -1 Then
CurrentDb.Execute updateBinWidthByProduct
ElseIf updateBinWidthMethod = 0 Then
CurrentDb.Execute updateBinWidthByBinType
End If
If Not IsNull([secondaryBinSelectBox]) Then
binID = Nz([secondaryBinSelectBox].[Column](1), 0)
binType = Nz([secondaryBinSelectBox].[Column](3), 0)
updateBinWidthMethod = Nz(DLookup("[calculate_bin_by_product]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 1)
requiredBinWidthByBinType = Nz(DLookup("[width_mm]", "[tblBinType]", "[bin_type] ='" & binType & "'"), 0)
updateBinWidthByProduct = "UPDATE tblBin SET [bin_width_mm] ='" & (requiredBinWidthByProduct) & "' WHERE [bin_id] = " & (binID)
updateBinWidthByBinType = "UPDATE tblBin SET [bin_width_mm] ='" & (requiredBinWidthByBinType) & "' WHERE [bin_id] = " & (binID)
If updateBinWidthMethod = -1 Then
CurrentDb.Execute updateBinWidthByProduct
ElseIf updateBinWidthMethod = 0 Then
CurrentDb.Execute updateBinWidthByBinType
End If
End If
setBinWidthMmTextBoxValues
End If
End Sub
Private Sub depthMM_AfterUpdate()
recalculateBinSize
End Sub
Private Sub masterBinSelectBox_AfterUpdate()
recalculateBinSize
End Sub
Private Sub secondaryBinSelectBox_AfterUpdate()
recalculateBinSize
End Sub
Private Sub rows_AfterUpdate()
recalculateBinSize
End Sub
Private Sub widthMM_AfterUpdate()
recalculateBinSize
End Sub