Automatically Select Record in ListBox

ktaccess

New member
Local time
Today, 16:15
Joined
Aug 24, 2007
Messages
1
1. I have a listbox on a form which returns the results from a Parameter Query (One where the user enters the value and it pulls multiple records matching the value entered.)

I would like, once this form runs, to have the first record selected or highlighted in the listbox results.

I want the form to do this because I want the user to be able to quickly copy one of the records to clipboard for pasting in another application. So all they have to do is run the query, and hit CTRL-C to copy the first record.

Can someone tell me how to do this, if it's possible?

2. Also, if there is a way to automatically copy the first record to clipboard, or a way so if they click a record it automatically copies it to clipboard, that would be great too.

Thanks so much for the help
 
I had to ask almost this exact question yesterday... the following lines of code, put into the form_load or form_open event should accomplish what you want to do:

lstResults.Selected(1) = True

The above line highlights the second item in the list. The first item in my list was actually a header. If you dont have a header, substitute the 1 for a 0 and the first item in the list will be selected.

lstResults.Value = lstResults.ItemData(1)

The above line actually updates the listbox's boundcolumn property to reflect the selection. This one stumped me until I got to these forums. Again, substitute the 1 for 0 if you need to.

Those two lines should do it for you. Let me know if it works for you application.

Thanks!
 
I had to ask almost this exact question yesterday... the following lines of code, put into the form_load or form_open event should accomplish what you want to do...
I believe you will find that the Open Event is too early in the process to reference a control on the form. You will need to use the OnLoad Event.
 
Hi @RuralGuy and @ogg13, I have a similar question. If I would like to select the list box items like 1,4,5,8,10, etc., Is there any possibility that we can use an array of numbers to select the items in the ListBox on Onload event of the form? I tried the below code, it is showing a type mismatch.

Code:
[Forms]![frm_Fields Selection]![LstBxSelected].Selected(Array(1, 4, 5, 9)) = True

Please help if there is any possibility to avoid more lines of code. Thank you in advance.
 
Hi @RuralGuy and @ogg13, I have a similar question. If I would like to select the list box items like 1,4,5,8,10, etc., Is there any possibility that we can use an array of numbers to select the items in the ListBox on Onload event of the form? I tried the below code, it is showing a type mismatch.

Code:
[Forms]![frm_Fields Selection]![LstBxSelected].Selected(Array(1, 4, 5, 9)) = True

Please help if there is any possibility to avoid more lines of code. Thank you in advance.
I do not believe so.?
Just loop and set the relevant index of the listbox using the values from the array? adjusting for base 0 if need be?
 

Users who are viewing this thread

Back
Top Bottom