Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a Row AND VALUE in a Listbox

Status
Not open for further replies.

grobermatic

Technical User
Dec 21, 2002
153
GB
Hi,

i'm using the following code to programatical;ly select a row in a listbox.

Code:
Function SelectTop(frm As Form, listbox As String, Optional OnOff As Boolean)
Dim lngCounter As Long 'set up counter variable
    
    For lngCounter = 0 To 1 [COLOR=green]'standard for/next loop[/color]
        frm(listbox).Selected(lngCounter) = OnOff [COLOR=green]'sets the .selected property to the passed boolean parameter[/color]
    Next lngCounter

End Function

However when the list item is selected, the value of the listbox doesn't get set or changed.

I have setup a textbox next to the listbox with its controlsource set to =[lstpayments]. When selecting the listbox items with the pointer, the textbox reflects this change by showing the value of the listbox.

Any ideas on how to make this happen when manipulating the listbox programatically???

Many Thanks


Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
Hallo,

You have to set the value of the list box.

Replace
For lngCounter = 0 To 1
frm(listbox).Selected(lngCounter) = OnOff
Next lngCounter
with
frm(listbox) = OnOff

assuming you just want to select either on or off, not both or neither.
You only need to worry about the Selected property if you have a multi-select list box.

- Frink
 
Hi Frink,

I've tried your suggestion but all that seems to happen is that it sets the value of the listbox to TRUE... reflected in the text box as it changes to -1

I want to select one of the rows and set the listbox value to the bound value of that row.

Any suggestion as to where I'm going wrong?

Cheers


Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
Hallo,

I was confused because your sample code seemes to select or deselect the first two entries of your listbox.

'I want to select one of the rows and set the listbox value to the bound value of that row.'

If you select a row in a listbox (not multi-selection), then the value of the listbox will be the value from the bound column of that row. That's the way list boxes work.

In code, either set the value of the list box to the required value, and the row bound to that value will automatically be selected, or
to set the nth value in the listbox, set the listbox to .column(.boundcolumn-1,n) or something like that.

The text box is fine as it is, but if it doesent update after changing the value programatically, use the .refresh or .recalc method.

- Frink
 
Frink,

Sorry for the delay...

I've had another play and I still can't get any decent results.

Sorry for any confusion before... I'm trying to programatically select a row in the listbox and then read the .value property, however its always NULL. If I click it manually then the value DOES change. The text box I mentioned is simply there as to monitor the listbox value and changes when the listbox is updated.

From what I can gather it should be as simple as :

Code:
listbox.selected(i) = true
(from access help and many threads on tek-tips)

However the value of the listbox never gets set using this method... am I missing something or do I need to set aomething else for this method to work??

All help appreciated

Thanks

Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
How are ya grobermatic . . . . .

[blue]Listboxes[/blue] are one one of the few Objects that [blue]don't behave properly[/blue].

First clear selections with:
Code:
[blue]   Me!ListBoxame.RowSource = Me!ListBoxame.RowSource

[purple][b]Note: Me!ListBoxame.Requery doesn't always work (if at all)![/b][/purple][/blue]
Then set your selection:
Code:
[blue]Me!ListBoxame.Selected(?) = True[/blue]

Calvin.gif
See Ya! . . . . . .
 
Hi TheAceMan1,

Thanks for your suggestion but the listbox is still only outputting a NULL value.

What do you think of setting the MultiSelect property to SIMPLE and using a FOR...NEXT loop to select only the top value (which is always the one I want).

I have used this method to "Select All" in another listbox in the same project and take action based on the selection... this works fine... can't understand why this one works and the simpler one doesn't though.

Thanks again


Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
I have managed to work around this problem by using the following code :



Code:
Function SelectTop(frm As Form, listbox As String, Optional OnOff As Boolean)

Dim lngCounter As Long 'set up counter variable
    
    For lngCounter = 1 To 1
        frm(listbox).Selected(lngCounter) = OnOff 'sets the .selected property to the passed boolean parameter
    Next lngCounter


End Function

-----------------------------------------------------------
...
SelectTop Forms(frmMainA.Name), "lstpayments", True
 For Each varCashItem In frmMainA!lstPayments.ItemsSelected
            strPaymentRef = frmMainA!lstPayments.ItemData(varCashItem)
 Next
...

not ideal but workable... if only access would work as it is supposed to!

Thanks one and all for your help... Merry Christmas!!


Craig

--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
grobermatic . . . . .

[blue]Selecting thru VBA[/blue] does not set the [purple]Index[/purple] of the listbox (needed to read the value). However
grobermatic said:
[blue] . . . using a FOR...NEXT loop to select only the top value ([purple]which is always the one I want[/purple]).[/blue]
Since [blue]you know what you want[/blue], why not use the [purple]Column[/purple] property:
Code:
[blue]=ListBox.Column(col,row)[/blue]

Calvin.gif
See Ya! . . . . . .
 
TheAceMan1,

Thanks for your help, I'll give this a go nexttime I'm at work.

Craig



--------------------------------------------------------------------------------------------------------
"Time-traveling is just too dangerous. Better that I devote myself to study the other great mystery of the universe: Women!" .. Dr E. Brown (1985)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top