Hiiiii 
I'm a complete and utter newbie to the world of VB programming within databases. I followed a textbook to create a database and have used a pile of code. However, the textbook is AWFUL at explaining what this code does...so I basically have a load of code within the database that I'm unsure of. Can any1 help me to understand what is going on? I'm not in any rush or anything, as I'm just doing this to try and learn some VB.
Many thanks in advance...
(sorry about the mass of code) 
Code:
Option Compare Database
Option Explicit
Dim rs As DAO.Recordset
Dim dtDOB As Date
Dim lngPeopleTypID As Long
Dim lngPeopleId As Long
Dim lngParentId As Long
Dim lngParentStudentId As Long
Dim strStartSql As String
Dim strWhereSql As String
Dim strSortOrder As String
Dim strSql As String
Dim strCurCtrlName As String
Dim strFilterVal As String
Dim strMsg As String
Dim varRecCnt, vbResponse
Function ShowHideCtrls(Status As Boolean)
Me.cboPeopleTypeID.Visible = Status
Me.cboGenderID.Visible = Status
Me.chkActive.Visible = Status
Me.cboTitleID.Visible = Status
Me.txtFirstName.Visible = Status
Me.txtMiddleName.Visible = Status
Me.txtSurname.Visible = Status
Me.txtAddressLine1.Visible = Status
Me.txtAddressLine2.Visible = Status
Me.txtTown.Visible = Status
Me.txtState.Visible = Status
Me.txtCounty.Visible = Status
Me.txtPostCode.Visible = Status
Me.txtTel.Visible = Status
Me.txtMobile.Visible = Status
Me.txtEMail.Visible = Status
Me.txtWeb.Visible = Status
Me.txtDateofBirth.Visible = Status
Me.txtAge.Visible = Status
If Me.cboPeopleTypeID = 2 Then 'Student
Me.cboGrade.Visible = Status
Else
Me.cboGrade.Visible = False
End If
Me.lblReqd0.Visible = Status
Me.lblReqd1.Visible = Status
Me.lblReqd2.Visible = Status
Me.lblReqd3.Visible = Status
Me.cmdUndo.Visible = Status
Me.cmdSave.Visible = Status
Me.cmdCancel.Visible = Status
Me.cmdClose.Enabled = Not Status
Me.lstPeople.Locked = Status
Me.cboPeopleType.Locked = Status
Me.lstStudentParents.Locked = Status
If Status = True Then
Me.cmdRemove.Enabled = Not Status
Me.cmdLinkParentToStudent.Enabled = Not Status
Me.cmdAddParent.Enabled = Not Status
End If
Me.lstParentsInDb.Locked = Status
Me.txtParentFilter.Locked = Status
If Status = True Then
If Me.txtParentFilter > "" Then
Me.cmdClrFltr.Enabled = True
Else
Me.cmdClrFltr.Enabled = False
End If
Else
Me.cmdClrFltr.Enabled = False
End If
End Function
Function SetParentListSql()
Dim lngParentInDbId As Long
lngPeopleId = Me.lstPeople
'strStartSql = "SELECT tblPeople.PeopleID, tblPeople!Surname & "", "" & tblPeople!FirstName & "" "" & " _
& "tblPeople!MiddleName AS Name, tblPeople.Town, tblPeople.Tel AS Phone, tblPeople.Mobile AS " _
& "Cell FROM tbluPeopleType RIGHT JOIN tblPeople ON tbluPeopleType.PeopleTypeID = " _
& "tblPeople.PeopleTypeID WHERE tblPeople.PeopleID Not In (SELECT tblPeople.PeopleID FROM " _
& "tblParentStudentLink INNER JOIN tblPeople ON tblParentStudentLink.ParentID = " _
& "tblPeople.PeopleID WHERE (((tblPeople.PeopleTypeID)=1) AND ((tblParentStudentLink.StudentID)=" _
& "" & lngPeopleId & "));) AND ((tblPeople.PeopleTypeID)=1"
strStartSql = "SELECT tblPeople.PeopleID, tblPeople!Surname & "", "" & tblPeople!FirstName & "" "" & " _
& "tblPeople!MiddleName AS Name, tblPeople.Town, tblPeople.Tel AS Phone, tblPeople.Mobile AS " _
& "Cell FROM tbluPeopleType RIGHT JOIN tblPeople ON tbluPeopleType.PeopleTypeID = " _
& "tblPeople.PeopleTypeID WHERE tblPeople.PeopleID Not In (SELECT tblPeople.PeopleID FROM " _
& "tblParentStudentLink INNER JOIN tblPeople ON tblParentStudentLink.ParentID = " _
& "tblPeople.PeopleID WHERE (((tblPeople.PeopleTypeID)=1) AND ((tblParentStudentLink.StudentID)=" _
& "" & lngPeopleId & "));) AND tblPeople.PeopleTypeID=1"
If Me.ActiveControl.Name = "txtParentFilter" Then
If Me.txtParentFilter.Text > "" Then
strFilterVal = Me.txtParentFilter.Text
strWhereSql = " AND tblPeople.Surname Like """ & strFilterVal & "*"""
Else
strWhereSql = ""
End If
End If
strSortOrder = " ORDER BY tblPeople.Surname;"
strSql = strStartSql & strWhereSql & strSortOrder
With Me.lstParentsInDb
.Value = Null
.RowSource = strSql
End With
End Function
Function ManageActiveLabel()
If Me.chkActive = True Then
Me.lblActive.Caption = "Active"
Else
Me.lblActive.Caption = "Inactive"
End If
End Function
Public Function SetPeopleListSql()
If Me.cboPeopleType = 2 Then 'Student selected
strStartSql = "SELECT tblPeople.PeopleID, tblPeople!Surname & "", "" & " _
& "tblPeople!FirstName & "" "" & tblPeople!MiddleName AS Name, " _
& "tblPeople.Town, tbluPeopleType.PeopleType AS Type, " _
& "tblPeople.Grade FROM tbluPeopleType RIGHT JOIN tblPeople " _
& "ON tbluPeopleType.PeopleTypeID=tblPeople.PeopleTypeID"
Else
strStartSql = "SELECT tblPeople.PeopleID, tblPeople!Surname & "", "" & " _
& "tblPeople!FirstName & "" "" & tblPeople!MiddleName AS Name, " _
& "tblPeople.Town, tbluPeopleType.PeopleType AS Type " _
& "FROM tbluPeopleType RIGHT JOIN tblPeople ON " _
& "tbluPeopleType.PeopleTypeID = tblPeople.PeopleTypeID"
End If
If Me.cboPeopleType > 0 Then
lngPeopleTypID = Me.cboPeopleType
strWhereSql = " WHERE tblPeople.PeopleTypeID = " & lngPeopleTypID & ""
Else
strWhereSql = ""
End If
If Me.ActiveControl.Name = "txtStudentFilter" Then
If Me.txtStudentFilter.Text > "" Then
strFilterVal = Me.txtStudentFilter.Text
If strWhereSql > "" Then
strWhereSql = strWhereSql & " AND tblPeople.Surname Like """ & strFilterVal & "*"""
Else
strWhereSql = " WHERE tblPeople.Surname Like " & strFilterVal & ""
End If
End If
End If
strSortOrder = " ORDER BY tblPeople.Surname;"
strSql = strStartSql & strWhereSql & strSortOrder
Me.lstPeople.RowSource = strSql
End Function
Function ManageSaveButton()
If Me.cboPeopleTypeID > 0 And Not IsNull(Me.txtFirstName) And Not IsNull(Me.txtSurname) Then
Me.cmdSave.Enabled = True
Else
Me.cmdSave.Enabled = False
End If
End Function
Function ManageDeleteButton()
'Note: Only People who have not been associated with other tables in the
' applicaiton can be deleted if they cannot be deleted they could be
' deactivated
'have to check two tables to see if the currently selected Id has been used
If Me.lstPeople > 0 Then
'read the selected ID value
lngPeopleId = Me.lstPeople
'check the "tblStudentClassesLink" table
'check the "tblParents" table
varRecCnt = DCount("PeopleId", "tblPeople", "PeopleId = " & lngPeopleId & "")
If varRecCnt > 0 Then
Me.cmdDelete.Enabled = False
Else
Me.cmdDelete.Enabled = True
End If
'varRecCnt = DCount("StudentsClassesID", "tblStudentClassesLink", "PeopleID = " & lngPeopleId & "")
strSql = "SELECT Count(tblStudentClassesLink.StudentClassID) AS CountOfStudentClassID " _
& "FROM tblStudentClassesLink WHERE (((tblStudentClassesLink.PeopleID)=" & lngPeopleId & "));"
Set rs = CurrentDb.OpenRecordset(strSql)
varRecCnt = rs.Fields("CountOfStudentClassID").Value
rs.Close
Set rs = Nothing
If varRecCnt > 0 Then
Me.cmdDelete.Enabled = False
Else
Me.cmdDelete.Enabled = True
End If
End If
End Function
Private Sub cboGenderID_AfterUpdate()
ManageSaveButton
Me.cmdUndo.Enabled = True
End Sub
Private Sub cboGenderID_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub cboGrade_AfterUpdate()
ManageSaveButton
End Sub
Private Sub cboGrade_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub cboPeopleType_AfterUpdate()
SetPeopleListSql
Me.lstPeople.Value = Null
Me.cmdEdit.Enabled = False
Me.cmdDelete.Enabled = False
End Sub
Private Sub cboPeopleTypeID_AfterUpdate()
ManageSaveButton
If Me.cboPeopleTypeID = 2 Then 'Student selected
Me.cboGrade.Visible = True
Else
Me.cboGrade.Visible = False
End If
End Sub
Private Sub cboPeopleTypeID_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub cboTitleID_AfterUpdate()
ManageSaveButton
Me.cmdUndo.Enabled = True
End Sub
Private Sub cboTitleID_Change()
Me.cmdUndo.Enabled = False
End Sub
Private Sub chkActive_AfterUpdate()
ManageActiveLabel
ManageSaveButton
Me.cmdUndo.Enabled = True
End Sub
Private Sub cmdAdd_Click()
DoCmd.GoToRecord , , acNewRec
ShowHideCtrls True
If Me.cboPeopleType > 0 Then
Me.cboPeopleTypeID = Me.cboPeopleType
Me.cboGenderID.SetFocus
Me.cboGrade.Visible = True
Else
Me.cboPeopleTypeID.SetFocus
End If
Me.cmdAdd.Enabled = False
Me.cmdEdit.Enabled = False
Me.cmdDelete.Enabled = False
Me.cmdAddParent.Enabled = False
End Sub
Private Sub cmdAddParent_Click()
DoCmd.OpenForm "frmManageParents", , , , acFormAdd, , "AddMode"
End Sub
Private Sub cmdCancel_Click()
If Me.Dirty Then
Me.Undo
End If
With Me.cmdAdd
.Enabled = True
.SetFocus
End With
If Me.lstPeople > 0 Then
Me.cmdEdit.Enabled = True
Me.cmdAddParent.Enabled = True
Else
Me.cmdEdit.Enabled = False
Me.cmdAddParent.Enabled = False
End If
ManageDeleteButton
ShowHideCtrls False
End Sub
Private Sub cmdClearFilter_Click()
With Me.txtStudentFilter
.SetFocus
.Value = Null
End With
Me.cmdClearFilter.Enabled = False
SetPeopleListSql
End Sub
Private Sub cmdClose_Click()
If Me.OpenArgs = "FromMainMenu" Then
If IsLoaded("frmMainMenu") Then
Forms!frmMainMenu.Visible = True
Else
DoCmd.OpenForm "frmMainMenu"
End If
ElseIf IsLoaded("frmAdminOps") Then
Forms!frmAdminOps.Visible = True
Else
DoCmd.OpenForm "frmAdminOps"
End If
DoCmd.Close acForm, "frmManageStudents"
End Sub
Private Sub cmdClrFltr_Click()
With Me.txtParentFilter
.SetFocus
.Value = Null
End With
Me.cmdClrFltr.Enabled = False
SetParentListSql
End Sub
Private Sub cmdDelete_Click()
If Me.lstPeople > 0 Then
lngPeopleId = Me.lstPeople
strMsg = "Are you sure you want to delete the selected individual?"
vbResponse = MsgBox(strMsg, vbQuestion + vbYesNo + vbDefaultButton1, "Delete Selected Person")
If vbResponse = vbYes Then
strSql = "DELETE * FROM tblPeople WHERE PeopleID = " & lngPeopleId & ";"
CurrentDb.Execute strSql
With Me.lstPeople
.Value = Null
.Requery
.SetFocus
End With
Me.cmdDelete.Enabled = False
Me.cmdEdit.Enabled = False
Me.cmdAddParent.Enabled = False
End If
End If
End Sub
Private Sub cmdEdit_Click()
If Me.lstPeople > 0 Then
lngPeopleId = Me.lstPeople
With Me.txtPeopleID
.Visible = True
.SetFocus
End With
DoCmd.FindRecord lngPeopleId
ShowHideCtrls True
Me.cboPeopleTypeID.SetFocus
Me.txtPeopleID.Visible = False
Me.cmdAdd.Enabled = False
Me.cmdEdit.Enabled = False
Me.cmdDelete.Enabled = False
Me.cmdAddParent.Enabled = False
End If
End Sub
Private Sub cmdLinkParentToStudent_Click()
If Me.lstPeople > 0 And Me.lstParentsInDb > 0 Then
lngPeopleId = Me.lstPeople
lngParentId = Me.lstParentsInDb
Set rs = CurrentDb.OpenRecordset("tblParentStudentLink")
rs.AddNew
rs.Fields("StudentID").Value = lngPeopleId
rs.Fields("ParentID").Value = lngParentId
rs.Update
Set rs = Nothing
With Me.lstStudentParents
.Value = Null
.Requery
End With
With Me.lstParentsInDb
.Value = Null
.Requery
End With
Me.lstPeople.SetFocus
Me.cmdLinkParentToStudent.Enabled = False
Me.cmdRemove.Enabled = False
End If
End Sub
Private Sub cmdRemove_Click()
If Me.lstStudentParents > 0 Then
lngParentStudentId = Me.lstStudentParents
strSql = "DELETE * FROM tblParentStudentLink " _
& "WHERE tblParentStudentLink.ParentStudentLinkID =" & lngParentStudentId & ";"
CurrentDb.Execute strSql
Me.lstPeople.SetFocus
With Me.lstStudentParents
.Requery
.Value = Null
End With
With Me.lstParentsInDb
.Requery
.Value = Null
End With
Me.cmdRemove.Enabled = False
Me.cmdLinkParentToStudent.Enabled = False
End If
End Sub
Private Sub cmdSave_Click()
If Me.Dirty Then
Me.Dirty = False
End If
With Me.cmdAdd
.Enabled = True
.SetFocus
End With
If Me.lstPeople > 0 Then
Me.cmdEdit.Enabled = True
Me.cmdAddParent.Enabled = True
Else
Me.cmdEdit.Enabled = False
Me.cmdAddParent.Enabled = False
End If
ManageDeleteButton
ShowHideCtrls False
Me.lstPeople.Requery
End Sub
Private Sub cmdUndo_Click()
If Me.Dirty Then
Me.Undo
End If
Me.cboPeopleTypeID.SetFocus
Me.cmdUndo.Enabled = False
Me.cmdSave.Enabled = False
End Sub
Private Sub Form_Current()
If Not IsNull(Me.txtDateofBirth) Then
dtDOB = Me.txtDateofBirth
Me.txtAge = Age(dtDOB, Date)
Else
Me.txtAge = ""
End If
ManageActiveLabel
Me.lstPeople.Requery
End Sub
Private Sub Form_Open(Cancel As Integer)
Select Case bytUserSecLevel
Case 1 'Admin User
Me.AllowAdditions = True
Me.AllowEdits = True
Me.AllowDeletions = True
Case 2 'Office Staff User
Me.AllowAdditions = True
Me.AllowEdits = True
Me.AllowDeletions = True
Case 3 'Teacher User
Me.AllowAdditions = False
Me.AllowEdits = False
Me.AllowDeletions = False
End Select
End Sub
Private Sub lstParentsInDb_AfterUpdate()
Me.cmdLinkParentToStudent.Enabled = True
End Sub
Private Sub lstPeople_AfterUpdate()
If Me.lstPeople > 0 Then
With Me.lstStudentParents
.Requery
.Visible = True
.Value = Null
End With
With Me.lstParentsInDb
.Requery
.Visible = True
.Value = Null
End With
Me.txtParentFilter.Visible = True
Me.cmdClrFltr.Visible = True
Me.cmdEdit.Enabled = True
Me.cmdAddParent.Visible = True
With Me.cmdLinkParentToStudent
.Visible = True
.Enabled = False
End With
With Me.cmdRemove
.Visible = True
.Enabled = False
End With
ManageDeleteButton
End If
End Sub
Private Sub lstPeople_DblClick(Cancel As Integer)
Call cmdEdit_Click
End Sub
Private Sub lstStudentParents_AfterUpdate()
If Me.lstStudentParents > 0 Then
Me.cmdRemove.Enabled = True
End If
End Sub
Private Sub txtAddressLine1_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtAddressLine1_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtAddressLine2_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtAddressLine2_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtCounty_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtCounty_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtDateofBirth_AfterUpdate()
If Not IsNull(Me.txtDateofBirth) Then
dtDOB = Me.txtDateofBirth
Me.txtAge = Age(dtDOB, Date)
End If
ManageSaveButton
End Sub
Private Sub txtDateofBirth_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtEmail_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtEMail_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtFirstName_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtFirstName_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtMiddleName_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtMiddleName_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtMobile_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtMobile_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtParentFilter_Change()
SetParentListSql
If Me.txtParentFilter.Text > "" Then
Me.cmdClrFltr.Enabled = True
Else
Me.cmdClrFltr.Enabled = False
End If
End Sub
Private Sub txtPostCode_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtPostCode_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtStudentFilter_Change()
SetPeopleListSql
Me.lstPeople.Value = Null
With Me.lstStudentParents
.Value = Null
.Visible = False
End With
With Me.lstParentsInDb
.Value = Null
.Visible = False
End With
With Me.txtParentFilter
.Value = Null
.Visible = False
End With
Me.cmdClrFltr.Visible = False
With Me.cmdRemove
.Default = False
.Visible = False
End With
With Me.cmdLinkParentToStudent
.Enabled = False
.Visible = False
End With
Me.cmdAddParent.Visible = False
If Me.txtStudentFilter.Text > "" Then
Me.cmdClearFilter.Enabled = True
Else
Me.cmdClearFilter.Enabled = False
End If
End Sub
Private Sub txtSurname_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtSurname_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtTel_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtTel_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtTown_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtTown_Change()
Me.cmdUndo.Enabled = True
End Sub
Private Sub txtWeb_AfterUpdate()
ManageSaveButton
End Sub
Private Sub txtWeb_Change()
Me.cmdUndo.Enabled = True
End Sub