I was quite pleased with this bit of code! It adds pairs of values to an unbound combobox on a form as you browse the records. I added it to the forms oncurrent event and a controls afterupdate.
It adds the id number and name of the current record to the combobox but doesnt duplicate any Id numbers. So if you keep switching between the same records it only adds them once. When the listrows gets to acertain number it deletes the first pair in the list. Clicking on a entry then takes me to that record. Feindishly clever eh! I hope it is useful to somebody.
Call MostRecentParent(Me, Me.parentname, Me.TxtEnquiryId)
Sub MostRecentParent(frm As Form, parentname, txtqueryid As Control)
'****** adds items to the most recent listbox
Dim strRecentEntry, NextEntry, SearchString, FindString, deletedString As String
Dim IsThere, SemiPos1, SemiPos2, StrLen As Integer
If Len(frm.parentname & "") > 0 Then 'no null values
SearchString = frm.ComboRecent.RowSource 'already in the combo
FindString = (txtqueryid) ' current enquiryid
IsThere = InStr(1, SearchString, FindString, vbTextCompare) 'is it there
If IsThere = 0 Then 'it isnt in the list so add it to combo
strRecentEntry = frm.ComboRecent.RowSource
NextEntry = txtqueryid & ";" & parentname & ";"
strRecentEntry = strRecentEntry & NextEntry
frm.ComboRecent.RowSource = strRecentEntry
End If
End If
'when the combo gets to a certain number then delete the first entry
If frm.ComboRecent.ListCount > 5 Then
StrLen = Len(frm.ComboRecent.RowSource)
SemiPos1 = InStr(1, SearchString, ";", vbTextCompare)
SemiPos2 = InStr(SemiPos1 + 1, SearchString, ";", vbTextCompare)
deletedString = Right(frm.ComboRecent.RowSource, StrLen - SemiPos2)
frm.ComboRecent.RowSource = deletedString
End If
'****** most recent listbox
End Sub
It adds the id number and name of the current record to the combobox but doesnt duplicate any Id numbers. So if you keep switching between the same records it only adds them once. When the listrows gets to acertain number it deletes the first pair in the list. Clicking on a entry then takes me to that record. Feindishly clever eh! I hope it is useful to somebody.
Call MostRecentParent(Me, Me.parentname, Me.TxtEnquiryId)
Sub MostRecentParent(frm As Form, parentname, txtqueryid As Control)
'****** adds items to the most recent listbox
Dim strRecentEntry, NextEntry, SearchString, FindString, deletedString As String
Dim IsThere, SemiPos1, SemiPos2, StrLen As Integer
If Len(frm.parentname & "") > 0 Then 'no null values
SearchString = frm.ComboRecent.RowSource 'already in the combo
FindString = (txtqueryid) ' current enquiryid
IsThere = InStr(1, SearchString, FindString, vbTextCompare) 'is it there
If IsThere = 0 Then 'it isnt in the list so add it to combo
strRecentEntry = frm.ComboRecent.RowSource
NextEntry = txtqueryid & ";" & parentname & ";"
strRecentEntry = strRecentEntry & NextEntry
frm.ComboRecent.RowSource = strRecentEntry
End If
End If
'when the combo gets to a certain number then delete the first entry
If frm.ComboRecent.ListCount > 5 Then
StrLen = Len(frm.ComboRecent.RowSource)
SemiPos1 = InStr(1, SearchString, ";", vbTextCompare)
SemiPos2 = InStr(SemiPos1 + 1, SearchString, ";", vbTextCompare)
deletedString = Right(frm.ComboRecent.RowSource, StrLen - SemiPos2)
frm.ComboRecent.RowSource = deletedString
End If
'****** most recent listbox
End Sub