Full Version: Select Item in Listbox
UtterAccess Forums > Microsoft® Access > Access Forms
zerotech
I was wondering if there was a way that when someone opens a form it automatically selects the first item in a list box. I know that the OnOpen needs to have some type of recordset to do this but I personally do not know that much about them. If anyone could please help me in this venture I would greatly appreciate it. Thanks.
ason
zerotech
OKay. It looks like I can do this with possibly the List3.Selected() function but everything I type in there seems to return an error. Any suggestions would be greatly appreciated. Thanks
ason
dannyseager
Me.List3.Selected(0) = True
Jack Cowley
Assume the list box has an ID as the first column (the bound column). If the first ID number is 3, for example, then place 3 in the Default Value property for the list box. Now the form will open with the first item selected.
th,
Jack
zerotech
Nevermind. Fingered it out. For other people's info in case needed:
OURLISTBOX = YOURLISTBOX.Selected(YOURLISTBOX.ListIndex + 0)
Simple. I was just making it harder than I wanted it to be.
Thanks,
Jason
dannyseager
Does that work Jack?
can't get it working on mine like that.
zerotech
Danny,
ours worked good to. I think I will stick to your code because mine was trial and error until something worked.
Thanks,
Jason
zerotech
Danny,
actually should say that yours works good except for the fact that now I can't move to a different selection. That could be a problem. LOL
Jason
Jack Cowley
Hmm. Works for me... In my case the bound column is a number (autonumber) and if the first record is 1 then putting 1 in the Default Value will highlight the first item in the list box. Be sure and save the form with the list box (after entering the Default Value) then open the form and see if the value is highlighted... It is for me....
ack
zerotech
Jack,

Whose works for you. If you are talking about Daniel's I can get the first record selected too but it won't let me change records (in the listbox).

Jason
Edited by: zerotech on Mon Oct 18 18:38:16 EDT 2004.
Jack Cowley
Jason -
I was using the Default Value method. Did it not work for you?
Jack
zerotech
Well it did. But like I said. I can't change the record in the Listbox. Actually I can't get anything to work on the page. It's like it locks the selection inside the listbox. Thanks for the help.
ason
zerotech
I think I have it working. What I had to do was put List3.SetFocus before I used List3.Selected(0). This seems to be working. I hope it doesn't crash like mine did. LOL.
Thanks,
Jason
Jack Cowley
Are you saying that if you put a value in the Default Value propertyof the List Box that everthing is 'locked up'? Just for fun add a new list box to the form using the Wizard. When the Wizard is finished open the list box in design view and put a number in the Default Value that is a valid number for you bound column. If, for example, your bound column is the RecordID and one of those numbers is 7 then use 7 as the Default Value. Close and save the form. Open the form. Is the record in the list box that has a RecordID of 7 highlighted? Can you click on another value in the list box?
ack
Jack Cowley
Jason -
ep, that will do the trick. Glad to hear you have it working...
Jack
dannyseager
glad I helped (if I did) frown.gif
Jack Cowley
Danny -
You always help!! That is why you are a VIP here at UA! Everyone appreciates all that you do to help other members....
Jack
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.