Oh yeah that's right. Half of the posts are missing. I'm sure one of the mods will sort it out anyway.
By the way, remember that the error handling needs to go into any function/sub that you suspect will error. To be honest, even if you don't anticipate it, best to have an error handler in place. And look for a way to use Allen Browne's error handling code instead of just the standard one you have. For me I have a whole class for error handling which spews it to a formatted text file.
At present I have injected the simple one into problematic codes (as a stop gap)
I want to eventually create one which will create log files/error files like yours when it occurs so I can then trace them easier. I will begin testing some of Allen Browne's code as soon as I feel confident enough. and of course will return here for help with it
Thanks so much. I won't mark this issue as solved but without the setfocus then I don't see how this error message could appear. BUT new errors may pop up.
The error handling I posted was written by ChrisO. It is good and simple.
When in design mode and you need to get rid of some code just Rem it out rather than delete it. you may find that the code was good so you may want it back.
Even thought I am in the change event for SearchFor and the code is setting the focus for SearchResults A totally different box?
This isn't my code of course, but it looks as if the Setfocus code is used to swap between the Searchfor (text box) and the SearchResults (list box)
I added the code with the code notes, please note the code isn't mine but modified from one of the guides from here
Code:
Private Sub SearchFor_Change()
On Error GoTo Err_SearchFor [COLOR=green]' Initialize error handling.[/COLOR]
[COLOR=green]'Create a string (text) variable[/COLOR]
Dim vSearchString As String
[COLOR=green]'Populate the string variable with the text entered in the Text Box SearchFor[/COLOR]
vSearchString = SearchFor.Text
[COLOR=green]'Pass the value contained in the string variable to the hidden text box SrchText,
'that is used as the sear4ch criteria for the Query QRY_SearchAll[/COLOR]
SrchText.Value = vSearchString
[COLOR=green]'Requery the List Box to show the latest results for the text entered in Text Box SearchFor[/COLOR]
Me.SearchResults.Requery
[COLOR=green]'Tests for a trailing space and exits the sub routine at this point
'so as to preserve the trailing space, which would be lost if focus was shifted from Text Box SearchFor
[/COLOR] If Len(Me.SrchText) <> 0 And InStr(Len(SrchText), SrchText, " ", vbTextCompare) Then
[COLOR=green] 'Set the focus on the first item in the list box
[/COLOR] Me.SearchResults = Me.SearchResults.ItemData(1)
Me.SearchResults.SetFocus
[COLOR=green] 'Requery the form to refresh the content of any unbound text box that might be feeding off the record source of the List Box[/COLOR]
DoCmd.Requery
[COLOR=green] 'Returns the cursor to the the end of the text in Text Box SearchFor,
'and restores trailing space lost when focus is shifted to the list box
[/COLOR] Me.SearchFor = vSearchString
Me.SearchFor.SetFocus
Me.SearchFor.SelStart = Me.SearchFor.SelLength
Exit Sub
End If
[COLOR=green]'Set the focus on the first item in the list box
[/COLOR] Me.SearchResults = Me.SearchResults.ItemData(1)
Me.SearchResults.SetFocus
[COLOR=green]'Requery the form to refresh the content of any unbound text box that might be feeding off the record source of the List Box
[/COLOR] DoCmd.Requery
[COLOR=green]'Returns the cursor to the the end of the text in Text Box SearchFor[/COLOR]
Me.SearchFor.SetFocus
If Not IsNull(Len(Me.SearchFor)) Then
Me.SearchFor.SelStart = Len(Me.SearchFor)
End If
Exit_SearchFor: [COLOR=green] ' Label to resume after error.
[/COLOR] Exit Sub [COLOR=green] ' Exit before error handler.
[/COLOR]Err_SearchFor: [COLOR=green] ' Label to jump to on error.[/COLOR]
MsgBox Err.Number & Err.Description [COLOR=green]' Place error handling here.[/COLOR]
Resume Exit_SearchFor [COLOR=green] ' Pick up again and quit.[/COLOR]
End Sub