amy420
asked on
Linking combo boxes
If I have two combo boxes that are both populated with different arrays from the same file. How do I link the two so that when I choose for example the last name in the first combo box, the first name automatically appears in the second combo box??
Can you show how the first and last names are stored in the program?
When you populate your combo box, add in the itemdata of each line, the unique item identifying the row in your table (usally an autonumber under Access), and the following code
Private Sub Combo1_Change()
Combo2.ListIndex = CBLFindCode(Combo1, Combo1.ItemData)
End Sub
Function CBLFindCode(CBL As Control, Code As Long) As Long
' #VBIDEUtils#************** ********** ********** ********** ********** ******
' * Programmer Name : Waty Thierry
' * Web Site : www.geocities.com/ResearchTriangle/6311/
' * E-Mail : waty.thierry@usa.net
' * Date : 12/08/98
' * Time : 17:40
' * Module Name : ListBox_Module
' * Module Filename : CBLSTBOX.BAS
' * Procedure Name : CBLFindCode
' * Parameters :
' * CBL As Control
' * Code As Long
' ************************** ********** ********** ********** ********** ****
' * Comments :
' * Search for a code stored in the itemdata
' * Return the listindex
' *
' *
' ************************** ********** ********** ********** ********** ****
Dim nIndex As Integer
Dim nCount As Integer
Dim Item As Long
nCount = CBL.ListCount - 1
CBLFindCode = -1
If (nCount = -1) Then
CBLFindCode = -1
Exit Function
End If
For nIndex = 0 To nCount
Item = CBL.ItemData(nIndex)
If Code = Item Then
CBLFindCode = nIndex
Exit For
End If
Next
End Function
Private Sub Combo1_Change()
Combo2.ListIndex = CBLFindCode(Combo1, Combo1.ItemData)
End Sub
Function CBLFindCode(CBL As Control, Code As Long) As Long
' #VBIDEUtils#**************
' * Programmer Name : Waty Thierry
' * Web Site : www.geocities.com/ResearchTriangle/6311/
' * E-Mail : waty.thierry@usa.net
' * Date : 12/08/98
' * Time : 17:40
' * Module Name : ListBox_Module
' * Module Filename : CBLSTBOX.BAS
' * Procedure Name : CBLFindCode
' * Parameters :
' * CBL As Control
' * Code As Long
' **************************
' * Comments :
' * Search for a code stored in the itemdata
' * Return the listindex
' *
' *
' **************************
Dim nIndex As Integer
Dim nCount As Integer
Dim Item As Long
nCount = CBL.ListCount - 1
CBLFindCode = -1
If (nCount = -1) Then
CBLFindCode = -1
Exit Function
End If
For nIndex = 0 To nCount
Item = CBL.ItemData(nIndex)
If Code = Item Then
CBLFindCode = nIndex
Exit For
End If
Next
End Function
ASKER
This is how I load them from the file:
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1
Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbofName.AddItem strfname(j)
Next
Loop
Close #1
End Sub
This does every thing but automatically fill the second combo box according to the first. Is this what you meant??
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1
Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbofName.AddItem strfname(j)
Next
Loop
Close #1
End Sub
This does every thing but automatically fill the second combo box according to the first. Is this what you meant??
Add This to your Code..
Private Sub cbolName_Click()
cbofName.ListIndex = cbolName.Listindex
End Sub
Private Sub cbofName_Click()
cbolName.ListIndex = cbofName.Listindex
End Sub
Private Sub cbolName_Click()
cbofName.ListIndex = cbolName.Listindex
End Sub
Private Sub cbofName_Click()
cbolName.ListIndex = cbofName.Listindex
End Sub
ASKER
This is how I load them from the file:
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1
Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbofName.AddItem strfname(j)
Next
Loop
Close #1
End Sub
This does every thing but automatically fill the second combo box according to the first. Is this what you meant??
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1
Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbofName.AddItem strfname(j)
Next
Loop
Close #1
End Sub
This does every thing but automatically fill the second combo box according to the first. Is this what you meant??
ASKER
They already have an array with the same number so adding more keys is not needed. TheAnswerMan helped me out. Thanks though..
AnswerMan, your solution is not correct, if the combo are all sorted (the contents won't never be in the same order except a big chance)
amy420, when you populate, do as following :
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
dim nLine as Long
Open App.Path & "\Names.Txt" For Input as #1
nLine = 0
Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbolName.Itemdata(cbolName .NewIndex) = nLine
cbofName.AddItem strfname(j)
cbofName.Itemdata(cbofName .NewIndex) = nLine
nLine = nLine + 1
Next
Loop
Close #1
End Sub
amy420, when you populate, do as following :
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
dim nLine as Long
Open App.Path & "\Names.Txt" For Input as #1
nLine = 0
Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbolName.Itemdata(cbolName
cbofName.AddItem strfname(j)
cbofName.Itemdata(cbofName
nLine = nLine + 1
Next
Loop
Close #1
End Sub
waty:
You beat me to it. However, you don't need the new variable ('nLine') you could just as easily use the value 'j'.
While we're on the subject...
amy420:
I see a potential problem with your loops. If there are less than 7 names, you will get a ReadPastEnd error.
If there are more than 7 names, the for/next loop will repeat possibly giving you a read past end error (if the number of names is not evenly divisable by 7).
Your code should read like this (adding waty's suggestion):
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1
j = 0
Do While Not EOF(1)
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbolName.Itemdata(cbolName .NewIndex) = j
cbofName.AddItem strfname(j)
cbofName.Itemdata(cbofName .NewIndex) = j
j = j + 1
Loop
Close #1
End Sub
You beat me to it. However, you don't need the new variable ('nLine') you could just as easily use the value 'j'.
While we're on the subject...
amy420:
I see a potential problem with your loops. If there are less than 7 names, you will get a ReadPastEnd error.
If there are more than 7 names, the for/next loop will repeat possibly giving you a read past end error (if the number of names is not evenly divisable by 7).
Your code should read like this (adding waty's suggestion):
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1
j = 0
Do While Not EOF(1)
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbolName.Itemdata(cbolName
cbofName.AddItem strfname(j)
cbofName.Itemdata(cbofName
j = j + 1
Loop
Close #1
End Sub
No one Said anything about the combobox Being sorted. It says..
I read them into the combos like this.. if that is the case..
I assume in the textfile that the First Name is the First name of the Same Lines last name.,
According to this.. each listindex will match up.
I read them into the combos like this.. if that is the case..
I assume in the textfile that the First Name is the First name of the Same Lines last name.,
According to this.. each listindex will match up.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I agree with you, they are always sequential...... Thanks