Couldn't help myself.... had to modify the code in the dB.
Are there multiple tables the maintenance data is stored in?
"FemcoPMLogData"
"HaasPMLogData"
"HardingePMLogData"
"DoosanPMLogData"
Or maybe they are queries?
Why is the event the "LostFocus" event. I would have used the "AfterUpdate" event.
And I would have the "RunDate" and "Machine" controls (text boxes) UNBOUND.
Because they are bound, the data in the two fields of the table "WorkFormData" changes for each search.
Anyway, here is the code:
Code:
Option Compare Database
Option Explicit
Private Sub Machine_LostFocus()
Dim CheckPMForMachineVar As Variant
Dim MachineNumberVar As String
Dim MachineDBVar As String
Dim MachineVariable As Byte
Dim ChkCriteriaVar As String
Dim msg As String
MachineVariable = Me.Machine
Select Case MachineVariable
Case 1 To 3
MachineDBVar = "FemcoPMLogData"
Case 4, 9
MachineDBVar = "HaasPMLogData"
Case 8, 10
MachineDBVar = "HardingePMLogData"
Case 11
MachineDBVar = "DoosanPMLogData"
Case Else
msg = "ERROR!!"
msg = msg & vbCrLf & vbCrLf
msg = msg & "Invalid machine number entered/selected!!"
msg = msg & vbCrLf & "Try again...."
MsgBox msg
Me.Machine = Null
Me.Machine.SetFocus
Exit Sub
End Select
CheckPMForMachineVar = DCount("PerformedOn", MachineDBVar, "[PerformedOn] = #" & Me.RunDate & "# AND " & "[Machine] = " & MachineVariable)
'************ COMMENT OUT when DEBUGGING complete *********************************
msg = "MachineVariable -> " & MachineVariable & vbCrLf
msg = msg & "MachineDBVar -> " & MachineDBVar & vbCrLf
msg = msg & "CheckPMForMachineVar -> " & CheckPMForMachineVar
MsgBox msg
'************ COMMENT OUT when DEBUGGING complete *********************************
ChkCriteriaVar = "[PerformedOn] = #" & Me.RunDate & "# AND " & "[Machine] = " & MachineVariable
Debug.Print ChkCriteriaVar
If CheckPMForMachineVar = 0 Then
msg = "It looks like Preventative Maintenance has not been performed on this machine today."
msg = msg & vbCrLf & vbCrLf
msg = msg & "Please remember to perform machine PMs as soon as possible."
MsgBox msg
' Else
' Exit Sub
End If
End Sub
BTW, in table "WorkFormData", the field name "Function" is a reserved word and shouldn't be used for object names.