Option Explicit
Dim lTotalRecords As Long
Private Enum CmdButtons
cmdMoveFirst = 0
cmdMovePrevious = 1
cmdMoveNext = 2
cmdMoveLast = 3
cmdAddNew = 4
cmdEdit = 5
cmdSave = 6
cmdDelete = 7
cmdUndo = 8
cmdFind = 9
cmdDone = 10
End Enum
Private Sub cmdButton_Click(Index As Integer)
Static vMyBookMark As Variant
Select Case Index
Case cmdMoveFirst
Data1.Recordset.MoveFirst
Call updateButtons
Case cmdMovePrevious
Data1.Recordset.MovePrevious
Call updateButtons
Case cmdMoveNext
Data1.Recordset.MoveNext
Call updateButtons
Case cmdMoveLast
Data1.Recordset.MoveLast
Call updateButtons
Case cmdAddNew '--add a new record
With Data1.Recordset
If (.EditMode = dbEditNone) Then
If (lTotalRecords > 0) Then
vMyBookMark = .Bookmark
Else
vMyBookMark = ""
End If
.AddNew
Call updateButtons
lblRecordCount = "Adding New Record"
End If
End With
Case cmdEdit '--edit the current record
With Data1.Recordset
If (.EditMode = dbEditNone) Then
vMyBookMark = .Bookmark
.Edit
Call updateButtons
lblRecordCount = "Editing"
End If
End With
Case cmdSave '--save the current record
Dim bMoveLast As Boolean
With Data1.Recordset
If (.EditMode <> dbEditNone) Then
If .EditMode = dbEditAdd Then
bMoveLast = True
Else
bMoveLast = False
End If
.Update
If (.EditMode = dbEditNone) Then
lTotalRecords = .RecordCount
If (bMoveLast = True) Then
.MoveLast
Else
.Move 0
End If
updateButtons True
End If
Else
.Move 0
End If
End With
Case cmdDelete '--delete the current record
Dim iResponse As Integer
Dim sAskUser As String
sAskUser = "Are you sure you want to delete this record?"
iResponse = MsgBox(sAskUser, vbQuestion + vbYesNo + _
vbDefaultButton2, "Personal Details Table")
If (iResponse = vbYes) Then
With Data1.Recordset
.Delete
lTotalRecords = .RecordCount
If (lTotalRecords > 0) Then
If lTotalRecords = 1 Then
.MoveFirst
ElseIf .BOF Then
.MovePrevious
End If
End If
End With
End If
Call updateButtons
Case cmdUndo '--undo changes to the current record
With Data1.Recordset
If (.EditMode <> dbEditNone) Then
.CancelUpdate
If (Len(vMyBookMark)) Then
.Bookmark = vMyBookMark
End If
updateButtons True
Else
.Move 0
End If
End With
Case cmdFind '--find a specific record
Dim iReturn As Integer
gFindString = ""
With frmFind
.addCaption = "Type Employee Surname to find"
.recordSource = "SELECT EmployeeSurname FROM Bank"
.Show vbModal
End With
If (Len(gFindString) > 0) Then
With Data1.Recordset
.FindFirst "EmployeeSurname = '" & gFindString & " ' "
If (.NoMatch) Then
iReturn = MsgBox("Employee Forename " & gFindString & _
" was not found.", vbCritical, "Personal")
Else
iReturn = MsgBox("Employee Name " & gFindString & _
" was retrieved.", vbInformation, "Personal")
End If
End With
End If
updateButtons
Case cmdDone '--exit the form
Load FrmMenu
FrmMenu.Show
Unload FrmRegistration2
End Select
End Sub
Private Sub Data1_Reposition()
With Data1.Recordset
lblRecordCount.Caption = "Bank Record " & (.AbsolutePosition + 1) & _
" of " & lTotalRecords
ProgressBar1.Value = .PercentPosition
If (Combo1.Visible) Then Combo1.SetFocus
End With
End Sub
Private Sub Form_Activate()
With Data1.Recordset
.MoveLast
lTotalRecords = .RecordCount
.MoveFirst
End With
updateButtons True
End Sub
Public Sub updateButtons(Optional bLockEm As Variant)
'------------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------------
'Position Button
' 0 move first
' 1 move previous
' 2 move next
' 3 move last
' 4 add a new record
' 5 edit the current record
' 6 save the current record
' 7 delete the current record
' 8 undo any changes
' 9 find a specific record
' 10 done. Unload the form
'--------------------------------------------------------------------------------------------------------
Select Case Data1.Recordset.EditMode
Case dbEditNone
If (lTotalRecords > 1) Then
If (Data1.Recordset.BOF) Or _
(Data1.Recordset.AbsolutePosition = 0) Then
navigateButtons ("00111101011")
ElseIf (Data1.Recordset.EOF) Or _
(Data1.Recordset.AbsolutePosition = lTotalRecords - 1) Then
navigateButtons ("11001101011")
Else
navigateButtons ("11111101011")
End If
ElseIf (lTotalRecords = 0) Then
navigateButtons ("0000100001")
End If
If (Not IsMissing(bLockEm)) Then
lockTheControls (bLockEm)
End If
Case dbEditInProgress
Call lockTheControls(False)
Combo1.SetFocus
navigateButtons ("00000010100")
Case dbEditAdd
Call lockTheControls(False)
navigateButtons ("00000010100")
Combo1.SetFocus
End Select
End Sub
Public Sub navigateButtons(sButtonString As String)
'-------------------------------------------------------------------------------------------------------
'-------------------------------------------------------------------------------------------------------
Dim iIndx As Integer
Dim iButtonLength As Integer
sButtonString = Trim$(sButtonString)
iButtonLength = Len(sButtonString)
For iIndx = 1 To iButtonLength
If (Mid$(sButtonString, iIndx, 1) = "1") Then
cmdButton(iIndx - 1).Enabled = True
Else
cmdButton(iIndx - 1).Enabled = False
End If
Next
DoEvents
End Sub
Public Sub lockTheControls(bLocked As Boolean)
Dim iIndx As Integer
With Screen.ActiveForm
For iIndx = 0 To .Controls.Count - 1
If (.Controls(iIndx).Tag = "1") Then
If (TypeOf .Controls(iIndx) Is TextBox) Then
If (bLocked) Then
.Controls(iIndx).Locked = True
.Controls(iIndx).BackColor = vbWhite
Else
.Controls(iIndx).Locked = False
.Controls(iIndx).BackColor = vbYellow
Text1(7).Locked = True
End If
End If
End If
Next
End With
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim iMessage As Integer
If (Data1.Recordset.EditMode <> dbEditNone) Then
iMessage = MsgBox("You must complete editing the current record", _
vbInformation, "Bank Details")
Cancel = True
End If
End Sub
Public Sub highLight()
With Screen.ActiveForm
If (TypeOf .ActiveControl Is TextBox) Then
.ActiveControl.SelStart = 0
.ActiveControl.SelLength = Len(.ActiveControl)
End If
End With
End Sub
Private Sub mnuAbout_Click()
Load FrmSplash2
FrmSplash2.Show
End Sub
Private Sub mnuexit_Click()
Unload Me
End Sub