Full Version: Listbox On Click Event Frustration
UtterAccess Forums > Microsoft® Access > Access Forms
HeroDaddy
Folks,
I have been going around in circles on this one:
I have 6 fields on a form, 5 that are text and are unbound and a listbox who's rowsource is generated by a sql statement. I am trying to have the form update the information in the 5 text fields when a line is selected in the listbox. I have the code working great except it seems to pull the data for the item previously selected. For example if I am on item 1 and move to item 2 the fields update however it pulls the data for item 1. I am using:
Dim vitem as variant
Dim sName as string
For each vitem in lst_box1.itemselected
sName = me.lst_box1.itemdata(vitem)
Next vitem
To retrieve the item selected but if I do a break on the code and check the value of my variable sName is the previous value not the new value selected by the user. It would seem the onClick event or mouseDown event fires before the listbox updates. The only way I can make it work is to use the onDoubleclick event or onExit event.
I am sure that either I am doing the hard way or missing something obvious so your expert advice as always is greatly appreciated.
Thank You
Jeff B.
It sounds like you're using the listbox to select a record, and the unbound controls to display that record's fields.
Is there a reason you aren't simply using a bound form?
After you make a (new) selection from the listbox, how is Access and/or the form to know that it needs to re-set the unbound controls?
It all starts with the data -- what's the underlying data for this?
HeroDaddy
The data is a single table that contains policies. The listbox is a sql query of that table listing the policyname and department. The intent of the form is to display a list of policies to choose from and then give the user a choice to perform several actions (printing, revising, etc) on the selected policy.
Is this what your looking for?
vtd
>>Listbox On Click Event
It sounds to me that the ListBox_AfterUpdate Event is more appropriate... Have you tried the AfterUpdate Event?
missinglinq
The AfterUpdate event is the correct event for this kind of thing. But I think the real problem is coming from the code you're using here.
The code that you're using is meant for situations where the Multi-Select Property is set to something other than None, and from your given scenario this Listbox should have the Multi-Select Property set to None.
If in the ListBox the fields appear, in order, as
Field1 Field2 Field3
the code would be
CODE
Private Sub YourListBoxName_AfterUpdate()
   Me.txtField1 =  Me.YourListBoxName.Column(0)
   Me.txtField2 = Me.YourListBoxName.Column(1)
   Me.txtField3= Me.YourListBoxName.Column(2)
End Sub

Notice that the column index is zero based.
Linq ;0)>
HeroDaddy
Thank you, your code correction solved the problem. I can easily see what I was doing wrong now.
nce again UA readers and contributors Rock!!!
missinglinq
Glad we could help!
Linq ;0)>
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.