i can't find the bug in this coding.
can't someone help me?
note: i have a table "tblCustDetails" and a form "FrmCustomerDetails"
I can't move on without finding the bug, it might occur in my other forms
Code:
Option Compare Database
Option Explicit
'Declaring Variables to be used
Dim dbsCustomer As DAO.Database
Dim rstCustomer As DAO.Recordset
Dim PresenceCheck As Boolean
'--------------------------------------------------------
'Purpose: to load Customer Details form with Customers's first record.
'Event : Form Load
'--------------------------------------------------------
Private Sub Form_Load()
'Executing database & recordset
Set dbsCustomer = CurrentDb
Set rstCustomer = dbsCustomer.OpenRecordset("tblCustDetails", dbOpenDynaset)
'To disable CustomerId textbox
Me.txtcustomerid = True
'set Search Client combox to nothing
Me.cmbSearchCustomer = ""
'Cycle to first record and display record
rstCustomer.MoveFirst
'Calling procedure to display Customer Record
Call displayRecord
End Sub
'-----------------------------------------------
'Purpose: Displaying record while updating search box
'Event : lookup value for combo box after update
'-----------------------------------------------
Private Sub cmbSearchCustomer_AfterUpdate()
With cmbSearchCustomer
'Pushing data to form from table
Me.txtcustomerid = .Column(0)
Me.txtsurname = .Column(1)
Me.txtname = .Column(2)
Me.txtaddress = .Column(3)
Me.txtrentalid = .Column(4)
Me.txtTelephone = .Column(5)
Me.txtMobile = .Column(6)
Me.txtoverdue = .Column(7)
Me.txtfine = .Column(8)
End With
End Sub
Private Sub cmbSearchCustomer_LostFocus()
'Set Search Combo Box to nothing
Me.cmbSearchCustomer = ""
End Sub
'---------------------------------
'Purpose: Cycle to previous record
'Event: On click previous button
'---------------------------------
Private Sub btnPrev_Click()
'Cycle to previous record
rstCustomer.MovePrevious
'Display message if current record is at beginning of file
If rstCustomer.BOF Then
rstCustomer.MoveFirst 'Remain at the first record
MsgBox "Cannot Cycle backwards. Already at the first record!", vbInformation + vbOKOnly, "Calliope Video store"
End If
Call displayRecord 'Display the data from Customer table
End Sub
'------------------------------
'Purpose: Cycle to next record
'Event: On click next record
'------------------------------
Private Sub btnNext_Click()
'Cycle to next record
rstCustomer.MoveNext
'Display message if current record is at end of file
If rstCustomer.EOF Then
rstCustomer.MoveLast
MsgBox "Cannot cycle forward. Already at the last record!", vbInformation + vbOKOnly, "Calliope Video Store"""
End If
Call displayRecord 'Display the data from Customer table
End Sub
'------------------------------
'Purpose: Add new record
'Event : On click add new record
'------------------------------
Private Sub btnNewRecord_Click()
'Set all fields to null
Call clearForm
'Enable the CustomerId text box
Me.txtcustomerid.Enabled = True
'set focus to CustomerId text box
Me.txtcustomerid.SetFocus
End Sub
'-------------------------------------------
'Purpose: Delete current record
'Event : On click delete button
'-------------------------------------------
Private Sub btnDeleteRecord_Click()
'Using local Variable
Dim DisplayMsg As String
'Confirmation message
DisplayMsg = MsgBox("Proceed with deletion of this record?", vbYesNo + vbQuestion, "Calliope Video Store")
'If input is yes
If DisplayMsg = vbYes Then
'Delete current record
With rstCustomer
.Delete 'delete current record
MsgBox "Current record deleted!", vbInformation + vbOKOnly, "Calliope Video Store"
.MoveNext 'Cycle to next record and display
If .EOF Then 'if at end of table
.MoveLast 'Stay to last record to avoid error
Call displayRecord 'display data into form
End If
Call displayRecord 'display data into form
End With
End If
End Sub
'-------------------------------------
'Purpose: to save new record
'Event Procedure: Save button on click
'-------------------------------------
Private Sub btnSaveRecord_Click()
PresenceChck = False
'Check if fields are empty by calling checkEmpty procedure
Call checkEmpty
'check if fields are empty
If PresenceCheck = True Then
'error message
MsgBox "Record cannot be saved as it contains blank fields!", vbExclamation + vbOKOnly, "XYZ Co. Ltd"
Exit Sub
Else 'Save record
rstClient.MoveLast 'Move to the last record
rstClient.AddNew 'add a new record
Call saveRecord 'Transfer the data from form to table
rstClient.Update 'update the table
MsgBox "The record has been saved successfully!", vbOKOnly + vbInformation, " XYZ Co. Ltd "
End If
End Sub
'------------------------------------------
'Purpose: to exit Client Form
'Event: On click, open Main Menu
'------------------------------------------
Private Sub btnMainMenu_Click()
'Close Customer Details Form
dbsClient.Close
'Close Customer Details record set
rstCustomer.Close
'Close Customer Form
DoCmd.Close
'Open Main Menu Form
DoCmd.OpenForm "frmMainMenu"
End Sub
'---------------------------------------------------------------
'Purpose: to display customer records from tblcustomer to Customer Form
'Event: User Defined prcedure
'----------------------------------------------------------------
Private Sub displayRecord()
'Display the data from table
With rstCustomer
Me.txtcustomerid = .Fields!CustID
Me.txtsurname = .Fields!CustSurname
Me.txtname = .Fields!CustName
Me.txtrentalid = .Fields!RentalID
Me.txtaddress = .Fields!Address
Me.txttelephoneno = .Fields!TelephoneNo
Me.txtmobileno = .Fields!MobileNo
Me.txtoverdue = .Fields!Overdue
Me.txtfine = .Fields!Fine
End With
End Sub
Private Sub clearForm()
'Clears all the text boxes in the form
Me.txtcustomerid = ""
Me.txtsurname = ""
Me.txtname = ""
Me.txtrentalid = ""
Me.txtaddress = ""
Me.txttelephoneno = ""
Me.txtmobileno = ""
Me.txtoverdue = ""
Me.txtfine = ""
Me.cmbSearchCustomer = ""
End Sub
'----------------------------------------------------
'Purpose: TO save all data from the form to the table
'Event: User Defined procedure
'----------------------------------------------------
Private Sub saveRecord()
'Save all data from the form into the table
With rstCustomer
.Fields!CustomerId = Me.txtcustomerid
.Fields!Surname = Me.txtsurname
.Fields!Name = Me.txtname
.Fields!RentalID = Me.txtrentalid
.Fields!Address = Me.txtaddress
.Fields!TelephoneNo = Me.txttelephoneno
.Fields!MobileNo = Me.txtmobileno
.Fields!Overdue = Me.txtoverdue
.Field!Fine = Me.txtfine
End With
End Sub
'---------------------------------------------------
'Purpose: Presence check done on empty fields
'Event: User Defined procedure
'---------------------------------------------------
Private Sub checkEmpty()
'Customer ID
If Me.txtcustomerid = "" Then
MsgBox "Please, enter a Customer Code!", vbOKOnly + vbExclamation, "Calliope Video Store"
PresenceCheck = True
Exit Sub
End If
'Surname
If Me.txtsurname = "" Then
MsgBox "Please enter Customer's Surname!", vbOKOnly + vbExclamation, "Calliope Video Store"
PresenceCheck = True
Exit Sub
End If
'Name
If Me.txtname = "" Then
MsgBox "Please enter Customer's Firstname", vbOKOnly + vbExclamation, "Calliope Video Store"
PresenceCheck = True
Exit Sub
End If
'Rental ID
If Me.txtrentalid = "" Then
MsgBox "Please enter Rental ID", vbOKOnly + vbExclamation, "Calliope Video Store"
PresenceCheck = True
Exit Sub
End If
'Address
If Me.txtaddress = "" Then
MsgBox "Please enter Customer's Address", vbOKOnly + vbExclamation, "XYZ Co. Ltd"
PresenceCheck = True
Exit Sub
End If
'Telephone Number
If Me.txttelephoneno = "" Then
MsgBox "Please enter Customer's Telephone Number", vbOKOnly + vbExclamation, "XYZ Co. Ltd"
PresenceCheck = True
Exit Sub
End If
'Mobile Number
If Me.txtmobileno = "" Then
MsgBox "Please enter Customer's Mobile Number", vbOKOnly + vbExclamation, "XYZ Co. Ltd"
PresenceCheck = True
Exit Sub
End If
'Overdue
If Me.txtoverdue = "" Then
MsgBox "Please state current status of customer's rental", vbOKOnly + vbExclamation, "XYZ Co. Ltd"
PresenceCheck = True
Exit Sub
End If
'Fine
If txtoverdue <> "" Then
If Me.txtfine = "" Then
MsgBox "Please state the amount of fine the customer has to pay if ", vbOKOnly + vbExclamation, "XYZ Co. Ltd"
PresenceCheck = True
Exit Sub
End If
End If
End Sub