Link to home
Start Free TrialLog in
Avatar of MMarson
MMarson

asked on

Linking ListBoxes together

I have two listboxes that get populated with records from an outside database.  I would like to have these two lists move and select as one list.  IE: when the users clicks on an item in listbox A, then the corresponding item in listbox B would be highlight (selected).  

Ideally, I would like to create one list box with two columns.  One column with field A and the second column with field B from one record.  Then I believe there would be a one-to-one relationship between these two fields.

Is there an easy way of creating what I would like??
ASKER CERTIFIED SOLUTION
Avatar of RichW
RichW
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vinnyd79
vinnyd79

Check out this page and look under API subclassing.

http://www.vbexplorer.com/VBExplorer/VB_Downloads.asp

Look for :
How two VB listboxes can be *linked*.
Use SendMessage API to create columns in a ListBox.

They are next to each other in the list.
just a suggestion: rather thatn using a LIST BOX, why not look into using a ListView Control, as you can then have as many columns as you need.

ListView is part of MS Common Controls (Project/Components)

Arthur Wood
To do just what you want,

In the List1_click event:
list2.listindex = list1.listindex
-------
in the list2_click event:
list1.listindex = list2.listindex
-------

This works as long as you have exactly the same number of entries and they are in the same order.
--------------
RichW has a pretty good solution for combining the info into one box. You don't really need to set the number of columns to 2 though. You still have the problem that you need to have exactly the same number of records.
--------------
If you have the possibility of having more records in one table than the other, we will have to get a bit more creative.
Avatar of Richie_Simonetti
i like Wood's sugeestion.
can u plzz post some part of the code if its not confidential!!
can u plzz post some part of the code if its not confidential!!
i was trying this option of placing two different sets of data in two columns of the same list box( was it this u have asked for?) i am able to selected the corresponding record in the second list box but unable to display the same record in the same listbox column 2. so if u let me know how far is it correct then i can proceed further... i did not finish with the code. i did a sample one.. to post to u. so let me whether my understanding of ur question is correct! thanks!!!
i was trying this option of placing two different sets of data in two columns of the same list box( was it this u have asked for?) i am able to selected the corresponding record in the second list box but unable to display the same record in the same listbox column 2. so if u let me know how far is it correct then i can proceed further... i did not finish with the code. i did a sample one.. to post to u. so let me whether my understanding of ur question is correct! thanks!!!
try this code!!

Public cn As New ADODB.connection
Dim rsTest As New ADODB.Recordset
Dim str As String

Private Sub Form_Load()
    Call connection
    str = "select teachername, teachercode from teachermaster "
    If rsTest.State = 1 Then rsTest.Close
    rsTest.Open str, cn
    List1.Columns = 1
rsTest.MoveFirst
Dim count
count = 0
While Not rsTest.EOF
    List1.List(count) = rsTest.Fields(0)
    List2.List(count) = rsTest.Fields(1)
    rsTest.MoveNext
    count = count + 1
Wend
End Sub
Function connection() As Boolean
    If cn.State = 1 Then cn.Close
    cn.Open "DSN=timetable"
    connection = True
    Exit Function
End Function

Private Sub List1_Click()
    If List1.Text <> "" Then
         i = List1.ListIndex
         List1.ListIndex = List2.ListIndex
         List2.ListIndex = i
    End If
End Sub


Avatar of MMarson

ASKER

I had already tried placing a tab between the two fields, and the problem I have is that the second field (column) does not lineup with the others now.  And using a proportional font makes it even harder.  

Unless there is some way to clear and set tabs within the list box??
Avatar of MMarson

ASKER

Arthur Wood,
I like your ListView option, but seem to be having a hard time setting up a ListView without icons.  I keep getting an initialization error.  
Any help??
Avatar of MMarson

ASKER

Arthur Wood,
I like your ListView option, but seem to be having a hard time setting up a ListView without icons.  I keep getting an initialization error.  
Any help??
Did you change the View property to report?  The setting would actually be lvwReport or number 3.