OK taz . . . . Here we go!
The problem is, we can't hide the ListBox because of code running in an event like On Lost Focus. [blue]So the Idea is to let the event complete and the focus go where ever it wants[/blue]. This is the function of the timer.
That done, we could now do what we want if we knew where the focus ran off to! I realized I could use the [blue]Screen Object[/blue] along with the [blue]ActiveControl property[/blue] to do just that! So lets get on with code. [purple]Don't forget to backup the DB before making any chages[/purple]. Also throughout this post, [purple]
LBN[/purple] = YourListBoxName & [purple]
TBN[/purple] = YourTextBoxName.
First we have to set a private constant to contain the delay time for the [blue]Timer Interval[/blue] event (this is the time in milliseconds). This is just a common location where you can change the value, instead of having to replace it many times in code (this is mainly for you, as you say you have more than one ListBox). So add the following to the [blue]Declarations Section[/blue] of your [blue]Form Module[/blue]:
Code:
[blue]Private Const MyDly As Long = 50 [green]'milliseconds[/green][/blue]
Next, the code for the [blue]On Timer[/blue] event. Its here the [blue]Screen Object[/blue] is used to tell which control has the focus. If the ListBox or the TextBox on top of it does not have the focus, the ListBox is hidden! What ever happens, the timer is always stopped here. Add the following to the form [blue]On Timer[/blue] event:
Code:
[blue] Dim curCtl As String
curCtl = Screen.ActiveControl.Name
If curCtl <> "[purple][b]LBN[/b][/purple]" And curCtl <> "[purple][b]TBN[/b][/purple]" Then
Me![purple][b]LBN[/b][/purple].Visible = False
End If
Me.TimerInterval = 0 [green]'Stop The Timer[/green][/blue]
Next, to activate the timer routine, we add [blue]myDly[/blue] to the [blue]On Lost Focus[/blue] event [purple]for both the TextBox & ListBox[/purple] with the following code:
Code:
[blue]Me.TimerInterval = MyDly[/blue]
So when either control loses focus, the timer starts running, the focus event completes, the cursor goes to some control, and the [blue]On Timer[/blue] routine takes over when the [blue]Timer Interval[/blue] runs out. [purple]
This is the key operation that makes it work![/purple]
A few more routines to complete operations . . . . .
Add the following code to the [blue]On Click[/blue] event of the TextBox:
Code:
[blue] Me![purple][b]LBN[/b][/purple].Visible = Not Me![purple][b]LBN[/b][/purple].Visible[/blue]
This simply toggles the ListBox between visible/hidden.
We still have to allow for making a selection in the ListBox. So add the following to the Listbox [blue]After Update[/blue] event:
Code:
[blue] Me![purple][b]TBN[/b][/purple] = Me![purple][b]LBN[/b][/purple].Column([green][b]1[/b][/green]) [green][b]'Correct Number For Your Column![/b][/green]
Me![purple][b]TBN[/b][/purple].SetFocus
Me![purple][b]LBN[/b][/purple].Visible = False[/blue]
One last optional item. ListBoxes have a nasty habit of showing a faint line under the first row. Add the following code to the forms [blue]On Open[/blue] event to take care of it:
Code:
[blue]Me![purple][b]LBN[/b][/purple].Requery[/blue]
Thats it taz. Give it a whirl and let me know!
See Ya! . . . . . .