Open form automatically when 1 record in Listbox

ScrmingWhisprs

I <3 Coffee Milk.
Local time
Today, 14:33
Joined
Jun 29, 2006
Messages
156
Hey there.
I have a search form where users can search for workers. The results are displayed in a listbox. From there, the user can double click on a name to open that worker's record. What I would like to happen is if there is only 1 result (a perfect match) I would like the worker's record to automatically open up. Here is my code:
Code:
Private Sub cmdSearch_Click()

 Me.lboResults.Requery
 If Me.lboResults.ListCount = 1 Then
 Me.lboResults.SetFocus
 DoCmd.OpenForm "frmWorkers", acNormal, , "[vID] = " & Me.lboResults
 End If
 
End Sub

The problem I'm having is it can't open the form because that record is not actually selected in the listbox. How would I go about automatically making the selection to the 1 record in the listbox?

Thanks
SW
 
Check out the ItemData() property of a listbox. It's essentially an array of the values in the bound columm of the control.
To select the first item in the control set its value to the first value in the array, which looks like...
Code:
Me.MyList = Me.MyList.ItemData(0)
...but knowing that, you don't even really need to select the first item anymore, you can use the value like this...
Code:
If Me.lboResults.ListCount = 1 Then
  DoCmd.OpenForm "frmWorkers", , , "[vID] = " & Me.lboResults.ItemData(0)
End If
 

Users who are viewing this thread

Back
Top Bottom