Problems closing a form in the GotFocus event

Evenjelith

Registered User.
Local time
Tomorrow, 06:07
Joined
Jul 14, 2008
Messages
11
Hey, I have a form (SearchName) which is passed a name from a different form (MainForm) and then queries to find any matches. Any name that matches or uses the same initial characters (e.g. Nick, Nicholas, Nicole if Nic were passed) is displayed in a listbox who's rowsource is a query.

I've written code that if the listbox length = 0 then the users is given a messagebox prompt telling them there were no matches and then the SearchName form should close. If the listbox length = 1 then there was only one match and rather then show them a listbox with one entry to select click that record is instead passed back to the MainForm and the SearchName form closed. Otherwise the listbox will populate and the user selects a name.

Both the methods dealing with length of the listbox list work with the exception of their close statments. I get an error:
"This action can't be carried out while processing a form or report event"
I would have thought this meant my query for populating the listbox is still running but if that were the case, how am i able to check the length of the array and identify zero, one or more elements present.

GotFocus seems the best place for putting my code since i dont want the form to appear before the checks are made and if possible id like to close it without it even ever appearing on the screen. In either case the user should not have to click anything on the SearchName form to get rid of it which is why I want to use the close methods.

Here is my code:

Private Sub List0_GotFocus()
If (List0.ListCount = "0") Then
MsgBox ("No matching well names found."), vbOKOnly, "No Match"
DoCmd.Close acForm, "SearchName"
End If

If (List0.ListCount = "1") Then
OnlyName = List0.ItemData(0)
DoCmd.OpenForm "MainForm"
DoCmd.GoToControl "NameMain"
DoCmd.Close acForm, "SearchName"
DoCmd.FindRecord OnlyWellName
End If

End Sub

MainForm is always open in the background. The name criteria is called NameMain in the MainForm.

Any help anyone can offer would be fantastic. Thanks
 
First of all a list count is a number, not a string so don't put it in quotes:

If (List0.ListCount = 0)
 
Thanks for that spot Bob, I've made the change. Appreciate your help. :)
 

Users who are viewing this thread

Back
Top Bottom