Select a row from a listbox

srb3464

New member
Local time
Today, 04:52
Joined
Feb 15, 2005
Messages
5
Could someone please help me do the following.

When I open my main form I would like the focus set on a listbox and the first row being selected.

I can get the focus set Me.List39.SetFocus but how do I select the first row?
 
A simple solution is to click on it with your pointer.

To have it happen automatic will require some code
 
Since one column in the listbox is bound, you can set the "default value" for the listbox to the first value of the bound field. Upon opening the form, the listbox will have focus and the default value will be selected. In the listbox properties, create an event procedure for "On Click" that includes the line "DoCmd.Requery" . That will fire the query that populates the form. Does this help?
 
Last edited:
Setting the default value of the control will work, but demands that you know this value in advance.
For a more flexible solution, use the ItemData property of the list box control, an indexed property that returns data from the bound column. Set the value of the control to the data in the first indexed position.

Code:
Private Sub Form_Open(Cancel As Integer)
   Me.List39 = Me.List39.ItemData(0)
End Sub
 
Even though this was not my original post, I'm quite interested, as I have the same need and had solved it by forcing the default. I did put in the recommended line of code for my listbox and when the form opens, I do see the top record surrounded by the dotted line, but the form data is empty. The form data in my design comes from a query that uses RecordNumber as the bound column in the listbox to retrieve the record from the master table. With a default value, the appropriate line (not the top) is hilited as black and the remaining form data is populated. which indicates the query has run. I appreciate any further clarification, as this problem has bugged me for a year on several applications that are similar. This is for charitable events and not for pay, so the users can't complain about how it presently works!
 
srb,

Select the first row:

Me.List39.Selected(0) = True

rw,

Just guessing, but I'd guess that you have a single-column Listbox, with the
Default value set to the 1st value of the list. The AfterUpdate of the
ListBox probably just does a Me.ReQuery

I don't really understand your question though.

Wayne
 

Users who are viewing this thread

Back
Top Bottom