Code:
Private Sub HorseName_Change()
'the purpose of this routine is to compare the characters typed into the horse name control
'to horse names that are already in tblHorses
'each new character typed is added to the previous character(s) typed such that the horse name is
'"constructed on the fly"
'each new character causes tblHorses to be searched again, only this time with the new character added
'when no matches are found the listboxes are closed and the user then is able to either continue
'typing in the name or move to the next field to enter further data
Dim strLastCharacter As String
Dim strCharacterCount As String
Dim strTempCopy As String
Dim strTemp As String
Dim frm As Form
Set frm = Me
strWhichCtrl = "NH"
'copy characters typed in form to temporary string
strTemp = frm!HorseName.Text
If strTemp = "" And SysCmd(acSysCmdGetObjectState, acForm, "frmNameList") > 0 Then
DoCmd.Close acForm, "frmNameList"
Exit Sub
End If
'determine the last character typed
strLastCharacter = Right(strTemp, 1)
'determine the total number of characters typed
strCharacterCount = Len(strTemp)
'If a 'space' is at the end of the string
'then append a question mark character after
'the 'space'
If strLastCharacter = " " Then
strTemp = strTemp & "?"
End If
'If a '?' is at the end of the string
'then get the contents of the string
'minus the '?'
If strLastCharacter = "?" Then
strTemp = Left(strTemp, (strCharacterCount - 1))
End If
strTempCopy = strTemp
Call frmNameList_Open(strTemp, strWhichCtrl, Me)
'make sure names are in proper case (first character of each name is upper case, remaing characters
'are lower case
strTempCopy = StrConv(strTempCopy, vbProperCase)
'the below code moves the focus back to HorseName, disables the OnChange event procedure, writes the
'current typed in text back to HorseName, positions the cursor after the last character in that text
'and re-enables the OnChange event procedure
frm.SetFocus
frm!HorseName.SetFocus
frm!HorseName.OnChange = ""
frm!HorseName.Text = strTempCopy
frm!HorseName.SelStart = strCharacterCount
frm!HorseName.OnChange = "[Event Procedure]"
Set frm = Nothing
End Sub
Public Sub frmNameList_Open(strSearchTerm As String, strWhoCalled As String, frm As Form)
'NameList listbox contains only individual horse, owner, rider or trainer names
'GroupList listbox contains the horse, owner, rider and trainer associated with a particular entry
' each 'group' is saved in tblGroups; the purpose of which is to prevent having to re-type the entire
' horse, owner, rider and trainer for each horse show
'the purpose of this code is to open 'frmNameList and to fill the NameList listbox and the
' GroupList listbox with names that have been filtered according to the SQL statements below
'a particular SQL string is built according to whether the user is entering a horse or pony, rider, owner or
'trainer name...that determination is based on the character passed to this routine in "strWhoCalled"
'called from:
' 1. frmEnterNewEntry - HorseName_Change event subroutine
' 2. frmEnterNewEntry - OwnerName_Change event subroutine
' 3. frmEnterNewEntry - RiderName_Change event subroutine
' 4. frmEnterNewEntry - TrainerName_Change event subroutine
' 5. frmChange - NewName_Change event subroutine
Select Case strWhoCalled
Case "CH" 'build Horse NameList SQL statement when frmEntryDetails has called this routine
strNameListSQL = "SELECT tblHorses.HorseID, tblHorses.HorseName, tblHorses.HorseSize " & _
"FROM tblHorses WHERE (((tblHorses.HorseName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblHorses.HorseName;"
Case "CO" 'build Owner NameList SQL statement when frmEntryDetails has called this routine
strNameListSQL = "SELECT tblOwners.OwnerID, tblOwners.OwnerName " & _
"FROM tblOwners WHERE (((tblOwners.OwnerName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblOwners.OwnerName;"
Case "CR" 'build Rider NameList SQL statement when frmEntryDetails has called this routine
strNameListSQL = "SELECT tblRiders.RiderID, tblRiders.RiderName, " & _
"tblRiders.AdultJunior FROM tblRiders WHERE (((tblRiders.RiderName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblRiders.RiderName;"
Case "CT" 'build Trainer NameList SQL statement when frmEntryDetails has called this routine
strNameListSQL = "SELECT tblTrainers.TrainerID, tblTrainers.TrainerName " & _
"FROM tblTrainers WHERE (((tblTrainers.TrainerName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblTrainers.TrainerName;"
Case "NH" 'build Horse NameList SQL statement
strNameListSQL = "SELECT tblHorses.HorseID, tblHorses.HorseName, tblHorses.HorseSize " & _
"FROM tblHorses WHERE (((tblHorses.HorseName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblHorses.HorseName;"
'build GroupList SQL statement
strGroupListSQL = "SELECT tblGroups.GroupID, tblGroups.HorseID, tblHorses.HorseName, " & _
"tblHorses.HorseSize, tblGroups.RiderID, tblRiders.RiderName, " & _
"tblRiders.AdultJunior, tblGroups.OwnerID, tblOwners.OwnerName, " & _
"tblGroups.TrainerID, tblTrainers.TrainerName " & _
"FROM tblTrainers INNER JOIN (tblRiders INNER JOIN (tblOwners INNER JOIN " & _
"(tblHorses INNER JOIN tblGroups ON tblHorses.HorseID = tblGroups.HorseID) ON " & _
"tblOwners.OwnerID = tblGroups.OwnerID) ON tblRiders.RiderID = " & _
"tblGroups.RiderID) ON tblTrainers.TrainerID = tblGroups.TrainerID " & _
"WHERE (((tblHorses.HorseName) Like """
strGroupListSQL = strGroupListSQL & strSearchTerm
strGroupListSQL = strGroupListSQL & "*""))ORDER BY tblHorses.HorseName;"
Case "NR" 'build Rider NameList SQL statement
strNameListSQL = "SELECT tblRiders.RiderID, tblRiders.RiderName, " & _
"tblRiders.AdultJunior FROM tblRiders WHERE (((tblRiders.RiderName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblRiders.RiderName;"
Case "NO" 'build Owner NameList SQL statement
strNameListSQL = "SELECT tblOwners.OwnerID, tblOwners.OwnerName " & _
"FROM tblOwners WHERE (((tblOwners.OwnerName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblOwners.OwnerName;"
Case "NT" 'build Trainer NameList SQL statement
strNameListSQL = "SELECT tblTrainers.TrainerID, tblTrainers.TrainerName " & _
"FROM tblTrainers WHERE (((tblTrainers.TrainerName) Like """
strNameListSQL = strNameListSQL & strSearchTerm
strNameListSQL = strNameListSQL & "*""))ORDER BY tblTrainers.TrainerName;"
End Select
DoCmd.OpenForm "frmNameList"
Forms!frmNameList.SetFocus
Forms!frmNameList!NameList.Visible = True
Forms!frmNameList!NameList.SetFocus
Forms!frmNameList!NameList.RowSource = strNameListSQL
DoCmd.Requery "NameList"
Forms!frmNameList!UnusedControl.SetFocus 'control's only purpose is to have somewhere to shift focus to
'if there are no matches to the SQL search then make NameList not visible
If Forms!frmNameList!NameList.ListCount < 2 Then
Forms!frmNameList!NameList.Visible = False
End If
Select Case strWhoCalled
Case "NH" 'NH = New horse
Forms!frmNameList!GroupList.Visible = True
Forms!frmNameList!GroupList.SetFocus
Forms!frmNameList!GroupList.RowSource = strGroupListSQL
DoCmd.Requery "GroupList"
Forms!frmNameList!UnusedControl.SetFocus
'if there are no matches to the SQL search then make GroupList not visible
If Forms!frmNameList!GroupList.ListCount < 2 Then
Forms!frmNameList!GroupList.Visible = False
End If
'if neither listbox is visible (no matches to the SQL searches) then close form
If Forms!frmNameList!NameList.Visible = False And _
Forms!frmNameList!GroupList.Visible = False Then
DoCmd.Close acForm, "frmNameList"
frm.SetFocus
End If
Case "NR", "NO", "NT", "CH", "CR", "CO", "CT"
'field calling is riders, owners or trainers, thus NameList is the only listbox
'to open and it was opened already, here we are just determining if there are
'any records to display, if not then close frmNameList
If SysCmd(acSysCmdGetObjectState, acForm, "frmNameList") > 0 And _
Forms!frmNameList!NameList.ListCount < 2 Then
DoCmd.Close acForm, "frmNameList"
frm.SetFocus
End If
End Select
End Sub
Probably not as efficient or elegant as the professionals would do but it works.