Option Compare Database
Option Explicit
Private Sub cmdClose_Click()
'Prompts the user to save the current record if it needs to be saved.
If Me.Dirty Then
MsgBox "Current T.O. has been modified." & vbCrLf & vbLf & "Please save the T.O.1", vbCritical + vbOKOnly, "Save Required"
Else
'Closes the Form
DoCmd.Close acForm, Me.Name
End If
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
MsgBox Err.Number, Err.Description
Resume Exit_cmdClose_Click
End Sub
Private Sub cmdFirst_Click()
On Error GoTo Err_cmdFirst_Click
'Prompts the user to save the current record if it needs to be saved.
If Me.Dirty Then
MsgBox "Current T.O. has been modified." & vbCrLf & vbLf & "Please save the T.O.2", vbCritical + vbOKOnly, "Save Required"
Else
'Go to First record
DoCmd.GoToRecord , , acFirst
'Updates the Work Log Summary
Me.lstSummary.Requery
End If
Exit_cmdFirst_Click:
Exit Sub
Err_cmdFirst_Click:
MsgBox Err.Description
Resume Exit_cmdFirst_Click
End Sub
Private Sub cmdLast_Click()
On Error GoTo Err_cmdLast_Click
'Prompts the user to save the current record if it needs to be saved.
If Me.Dirty Then
MsgBox "Current T.O. has been modified." & vbCrLf & vbLf & "Please save the T.O.3", vbCritical + vbOKOnly, "Save Required"
Else
'Go to Last record
DoCmd.GoToRecord , , acLast
'Updates the Work Log Summary
Me.lstSummary.Requery
End If
Exit_cmdLast_Click:
Exit Sub
Err_cmdLast_Click:
MsgBox Err.Description
Resume Exit_cmdLast_Click
End Sub
Private Sub cmdNew_Click()
On Error GoTo Err_cmdNew_Click
'Prompts the user to save the current record if it needs to be saved.
If Me.Dirty Then
MsgBox "Current T.O. has been modified." & vbCrLf & vbLf & "Please save the T.O.4", vbCritical + vbOKOnly, "Save Required"
Else
'Adds a new record
DoCmd.GoToRecord , , acNewRec
'Updates the Work Log Summary
Me.lstSummary.Requery
End If
Exit_cmdNew_Click:
Exit Sub
Err_cmdNew_Click:
MsgBox Err.Description
Resume Exit_cmdNew_Click
End Sub
Private Sub cmdNext_Click()
On Error GoTo Err_cmdNext_Click
'Prompts the user to save the current record if it needs to be saved.
If Me.Dirty Then
MsgBox "Current T.O. has been modified." & vbCrLf & vbLf & "Please save the T.O.5", vbCritical + vbOKOnly, "Save Required"
Else
'Go to Next Record
DoCmd.GoToRecord , , acNext
'Updates the Work Log Summary
Me.lstSummary.Requery
End If
Exit_cmdNext_Click:
Exit Sub
Err_cmdNext_Click:
MsgBox Err.Description
Resume Exit_cmdNext_Click
End Sub
Private Sub cmdPrevious_Click()
On Error GoTo Err_cmdPrevious_Click
'Prompts the user to save the current record if it needs to be saved.
If Me.Dirty Then
MsgBox "Current T.O. has been modified." & vbCrLf & vbLf & "Please save the T.O.6", vbCritical + vbOKOnly, "Save Required"
Else
'Go to Previous Record
DoCmd.GoToRecord , , acPrevious
'Updates the Work Log Summary
Me.lstSummary.Requery
End If
Exit_cmdPrevious_Click:
Exit Sub
Err_cmdPrevious_Click:
MsgBox Err.Description
Resume Exit_cmdPrevious_Click
End Sub
Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click
'Required fields the user has to enter before being able to save
If IsNull(txtTONumber) Then
MsgBox "T.O. Number must be entered before you can save the record.", vbCritical, "Invalid Save"
txtTONumber.SetFocus
ElseIf IsNull(cboProductDirectorate) Then
MsgBox "Product Directorate must be entered before you can save the record.", vbCritical, "Invalid Save"
cboProductDirectorate.SetFocus
Else
'Prompts the user to save the current record or cancel or undo changes made
Select Case MsgBox("Do you want to save your changes to the T.O.?" & vbCrLf & vbLf & "Yes: Save Changes" & vbCrLf & vbLf & "No: Do NOT Save Changes" & vbCrLf & vbLf & "Cancel: Undo Changes" & vbCrLf, vbYesNoCancel + vbQuestion, "Save Current T.O.?")
Case vbYes: 'Save the changes
Me.tbProperSave.Value = "Yes"
DoCmd.RunCommand acCmdSaveRecord
Me.tbProperSave.Value = "No"
Case vbNo: 'Do not save or undo
'Do nothing
Case vbCancel: 'Undo the changes
DoCmd.RunCommand acCmdUndo
Me.tbProperSave.Value = "No"
Case Else: 'Default case to trap any errors
'Do nothing
End Select
End If
Exit_cmdSave_Click:
Exit Sub
Err_cmdSave_Click:
If Err = 2046 Then 'The command or action Undo is not available now
Exit Sub
Else
MsgBox Err.Number, Err.Description
Resume Exit_cmdSave_Click
End If
End Sub
Private Sub cmdTracking_Click()
On Error GoTo Err_cmdTracking_Click
Dim stDocName As String
stDocName = "TrackingLog"
DoCmd.OpenReport stDocName, acPreview
Exit_cmdTracking_Click:
Exit Sub
Err_cmdTracking_Click:
MsgBox Err.Description
Resume Exit_cmdTracking_Click
End Sub
Private Sub cmdUndo_Click()
On Error GoTo Err_cmdUndo_Click
'Resets the record if it has been modified by the user.
If Me.Dirty Then
Beep
DoCmd.RunCommand acCmdUndo
Me.tbProperSave.Value = "No"
Else
Beep
MsgBox "There were no modifications made to the current record.", vbInformation, "Invalid Undo"
End If
Exit_cmdUndo_Click:
Exit Sub
Err_cmdUndo_Click:
MsgBox Err.Number, Err.Description
Resume Exit_cmdUndo_Click
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate
If Me.tbProperSave.Value = "No" Then
Beep
MsgBox "Please save the T.O.7", vbCritical + vbOKOnly, "Save Required"
DoCmd.CancelEvent
Exit Sub
End If
Exit_Form_BeforeUpdate:
Exit Sub
Err_Form_BeforeUpdate:
If Err = 3020 Then 'Update or CancelUpdate without AddNew or Edit
Exit Sub
Else
MsgBox Err.Number, Err.Description
Resume Exit_Form_BeforeUpdate
End If
End Sub
Private Sub Form_Current()
On Error GoTo Err_Form_Current
Me!txtTONumber.SetFocus
Me.tbProperSave.Value = "No"
'Displays the current record number
Me!txtCurrent = Me.CurrentRecord
Me.RecordsetClone.MoveLast
Me!txtTotal = RecordsetClone.RecordCount + Abs(Me.NewRecord) 'Displays the total number of records
Dim recClone As Object
Dim intNewRecord As Integer
' If this is a "New Record" then
' Disable the <Next>, <New>, <Last> buttons
' Enable the <First> and <Previous> buttons
' Then Exit the procedure.
If Me.NewRecord Then
cmdFirst.Enabled = True
cmdNext.Enabled = False
cmdPrevious.Enabled = True
cmdLast.Enabled = False
cmdNew.Enabled = False
Me.txtTONumber.SetFocus ' Set focus to the TO Number if a "New Record"
Exit Sub
Else
' Else if this is not a new record
' Enable <New> and <Last> buttons
cmdNew.Enabled = True
cmdLast.Enabled = True
End If
' Make a clone of the recordset underlying the form so
' we can move around without affecting the form's recordset
Set recClone = Me.RecordsetClone
' Check to see if there are no records
' If so disable all buttons except for the <New> button
If recClone.RecordCount = 0 Then
cmdNext.Enabled = False
cmdPrevious.Enabled = False
cmdFirst.Enabled = False
cmdLast.Enabled = False
Else
' Synchronise the current pointer in the two recordsets
recClone.Bookmark = Me.Bookmark
' If there are records, see if recordset is on the first record
' If so, disable the <First> and <Previous> buttons
recClone.MovePrevious
cmdFirst.Enabled = Not (recClone.BOF)
cmdPrevious.Enabled = Not (recClone.BOF)
recClone.MoveNext
' And then check whether recordset is on the last record
' If so, disable the <Last> and <Next> buttons
recClone.MoveNext
cmdLast.Enabled = Not (recClone.EOF)
cmdNext.Enabled = Not (recClone.EOF)
recClone.MovePrevious
End If
recClone.Close
Exit_Form_Current:
Exit Sub
Err_Form_Current:
If Err = 3021 Then
' Error 3021 means recordset is at Add New Record
' Enable <Previous> and <First> buttons
' Disable <Next> and <Last> buttons
cmdPrevious.Enabled = True
cmdFirst.Enabled = True
cmdNext.Enabled = False
cmdLast.Enabled = False
Resume Exit_Form_Current
Else
MsgBox Err.Description
Resume Exit_Form_Current
End If
End Sub
Private Sub frmTOItems_Enter()
'If TO Number is blank cannot go to subform
If IsNull(Me![txtTONumber]) Then
MsgBox "Enter TO Number before entering Item Details.", vbCritical, "Enter T.O. Number"
Forms![frmTONumber]![txtTONumber].SetFocus
End If
End Sub
Private Sub txtCurrent_AfterUpdate()
'Displays the current record number and also allows the user to enter a record number to go to
If IsNumeric(Me!txtCurrent) Then
If CLng(Me!txtCurrent) >= 0 And CLng(Me!txtCurrent) <= Me!txtTotal Then
Me.RecordsetClone.AbsolutePosition = Me!txtCurrent - 1
Me.Bookmark = Me.RecordsetClone.Bookmark
Else
Me!txtCurrent = Me.CurrentRecord
End If
Else
Me!txtCurrent = Me.CurrentRecord
End If
'Updates the Work Log Summary
Me.lstSummary.Requery
End Sub