List Box quandary (1 Viewer)

BrettM

just a pert, "ex" to come
Local time
Today, 14:55
Joined
Apr 30, 2008
Messages
134
Hi again guys,

I have a simple form linked to a table.
The table has 3 fields - ID, Name, Value.
The form has two Text Boxes - one for Name and one for Value.
It also has a List Box which has 3 columns - Name, Value and ID, bound to column 1.
I have set up the List Box using the "Find a record on my form" wizard and it works well. If I click on any line in the List Box then the two Text Boxes are updated with the correct data.

The List Box AfterUpdate has...
Code:
    Dim rs As Object
    Set rs = Me.Recordset.Clone
    rs.FindFirst "Name = '" & Me.ListBox1 & "'"
    If Not rs.EOF Then Me.Bookmark = rs.Bookmark

I would like to have the List Box show the current record when using the forms navigation buttons. I can achieve this by setting the List Box's control source to "Name" and removing the AfterUpdate code. This also works well.

Here is the quandary...

Having both the control source AND the After Update running at the same time causes some weird stuff. If you have the current record at say the 5th and click on the 3rd record in the List Box, suddenly the name of the 3rd record becomes the name of the 5th. Click around a few more times and everything is truly messed up.

Do you have any suggestions as to how to have this List Box being able to display the current record AND act as a record selector? I can make it do either but not both.

Thanks
Brett
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:55
Joined
Feb 19, 2002
Messages
43,266
It seems to me that ID is most likely the key of your table. If that is the case, the ID field is what you should be searching on. Also, Name and Value are really bad choices for column names since both are property names and will cause conflicts if you have to use any VBA. Never use function or property names as the names for columns or controls.
 

BrettM

just a pert, "ex" to come
Local time
Today, 14:55
Joined
Apr 30, 2008
Messages
134
Thanks for the reply Pat.

The names I used were purely for ease of reference here.

The issue seems to be a part of how a list box uses its Control Source in that it cannot be controlled AND BE the control at the same time.

I have now removed the Control Source altogether so the List box finds the record without mucking up the data (which happens the minute the box has a control source).

I now make the form set the box to the correct position with a simple little routine...
For X = 0 To Me.ServiceList.ListCount
If Me.ID = Val(Me.ServiceList.Column(0, X)) Then Exit For
Next X
Me.ServiceList.Selected(X) = True

This is called when the forms current record changes.

All is working well.

Thanks for the look guys.
Regards Brett
 

Users who are viewing this thread

Top Bottom